rem.js

 

 

/*
**author:Beginner
**create:20190620
**email:BeginnerMind@163.com
*/
var vRem;//全局变量添加vRem,记录当前状态下的rem值,方便js调用。
(function(win) {
    var designWidth=375,maxWidth=750,def_px=100,
        doc = win.document,docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc=function(){
            console.log('rem init');
            var width = docEl.getBoundingClientRect().width;
            width=width>maxWidth?maxWidth:width;
            vRem=Math.floor(width/designWidth*def_px);
            document.getElementsByTagName("html")[0].style.cssText = 'font-size: ' + vRem +"px";
        }
    recalc();
    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(window);

  

 

posted @ 2019-06-06 17:41  l_strive  阅读(109)  评论(0编辑  收藏  举报