px转换rem自适应移动端屏幕

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 = 64px;
            //可以根据实际需要修改
            docEl.style.fontSize = 64 * (clientWidth / 640) + 'px';
        };
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
}
fun(document, window);

猜你喜欢

转载自blog.csdn.net/cyx1874/article/details/83272820

相关文章