阅读 53

rem布局所需js

var fun = function (doc, win) {

var docEl = doc.documentElement,

    resizeEvt = 'orientationchange' in window ? 'orientationchange' :
    'resize',
    
    recalc = function () {
    
        var clientWidth = docEl.clientWidth;
        
        if (!clientWidth) return;
        
        //这里是假设在640px宽度设计稿的情况下,1rem = 20px;
        //可以根据实际需要修改
        docEl.style.fontSize = 20 * (clientWidth / 640) + 'px';
    };
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
复制代码

}

fun(document, window);