坏蛋格鲁坏蛋格鲁

【Function】JS 封装移动端 rem 适配时的初始化函数


function remLayoutInitial() {
    var timer = null;
    var PAGE_WIDTH = 750; // 设计稿的宽度 
    var PAGE_FONT_SIZE = 100; // 设计稿1rem的px大小

    function onResize() {
        var e = PAGE_FONT_SIZE * document.documentElement.clientWidth / PAGE_WIDTH;
        document.documentElement.style.fontSize = e + 'px';

        // 二次计算缩放像素,解决移动端webkit字体缩放bug
        var realitySize = parseFloat(window.getComputedStyle(document.documentElement).fontSize);
        if (e !== realitySize) {
            e = e * e / realitySize;
            document.documentElement.style.fontSize = e + 'px';
        }
    }

    window.addEventListener('resize', function() {
        if (timer) clearTimeout(timer);
        timer = setTimeout(onResize, 100);
    });

    onResize();
}
本原创文章未经允许不得转载 | 当前页面:坏蛋格鲁 » 【Function】JS 封装移动端 rem 适配时的初始化函数

评论