rem 自适应布局 js 代码

在前面head里面加上这一句禁止缩放:
 
 <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
 
以下是js代码 该代码1rem=100px :
 
!(function(win, doc) {
    function setFontSize() {
        var winWidth = window.innerWidth;
        doc.documentElement.style.fontSize = (winWidth / 1080) * 100 + 'px';
    }
    var evt = 'onorientationchange' in win ? 'orientationchange' : 'resize';
    var timer = null;
    win.addEventListener(evt, function() {
        clearTimeout(timer);
        timer = setTimeout(setFontSize, 300);
    }, false);
    win.addEventListener("pageshow", function(e) {
        if (e.persisted) {
            clearTimeout(timer);
            timer = setTimeout(setFontSize, 300);
        }
    }, false);
    //初始化 
    setFontSize();
}(window, document));

猜你喜欢

转载自www.cnblogs.com/an2333/p/11608487.html
今日推荐