假设根字体font-size的值是40px, 640/40=16,16就是px换算rem的值
function initHtmlFontSize(){
//获取可可视屏幕的宽度
var _width=document.body.clientWidth;
//判断
_width= _width > 640 ? 640:_width;
//当前的font-size的值
var _fs=_width/16;
//赋给html节点
document.getElementsByTagName('html')[0].style.fontSize=_fs + 'px';
}
initHtmlFontSize()
经过@天桥残局的优化顿时有深度有高度局长daio,上代码:
!(function(doc, win) {
var docEle = doc.documentElement,
evt = "onorientationchange" in window ? "orientationchange" : "resize",
fn = function() {
var width = docEle.clientWidth;
width && (docEle.style.fontSize = doc.documentElement.clientWidth/16 + "px");
};
win.addEventListener(evt, fn, false);
doc.addEventListener("DOMContentLoaded", fn, false);
}(document, window));