前端适配方案 rem
使用自执行函数 动态计算根元素 fontSzie
; (function (win, doc) {
function setFontSize() {
// 获取window 宽度
var winWidth = window.innerWidth;
doc.documentElement.style.fontSize = (winWidth / 1920) * 16 + ‘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));