// 根据屏幕大小自动调整document的字体大小 (function (doc, win) { var docEl = doc.documentElement; var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'; var scale = 1; // if([320,360,375,414,640].indexOf(docEl.clientWidth) >= 0) return; var recalc = function () { var width = docEl.clientWidth; if(width >= 750){ docEl.style.fontSize = '37.5px'; document.getElementsByTagName('html')[0].style.fontSize = '37.5px'; }else{ docEl.style.fontSize = width / 20 * scale + 'px'; document.getElementsByTagName('html')[0].style.fontSize = width / 20 * scale + 'px'; } }; recalc(); // 解决华为手机的rem值偏小问题 document.getElementsByTagName('head')[0].style.width = '20rem'; document.getElementsByTagName('head')[0].style.height = '0px'; document.getElementsByTagName('head')[0].style.display = 'block'; if(document.getElementsByTagName('head')[0].offsetWidth < docEl.clientWidth){ scale = docEl.clientWidth/document.getElementsByTagName('head')[0].offsetWidth; recalc(); document.getElementsByTagName('head')[0].style.display = 'none'; } if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); })(document, window);