手机REM使用代码

来源:互联网 发布:自考师范本科 知乎 编辑:程序博客网 时间:2024/05/17 23:55
在手机端使用rem单位时,需要根据不同的屏幕大小来设置html 字体大小,通过以下代码来实现
(function (){    var _self = this;    _self.width = 720;//设置默认最大宽度    _self.fontSize = 100;//默认字体大小    _self.widthProportion = function(){        var p = (document.body&&document.body.clientWidth||                document.getElementsByTagName("html")[0].offsetWidth)/_self.width;        return p>1?1:p<0.3?0.3:p;    };    _self.changePage = function(){       document.getElementsByTagName("html")[0].setAttribute("style","font-size:"+_self.widthProportion()*_self.fontSize+"px !important");    }    _self.changePage();    window.addEventListener("resize",function(){_self.changePage();},false);})();