(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth >= 750) {
docEl.style.fontSize = '100px';
} else {
docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
}
var html = document.getElementsByTagName('html')[0];
var settedFs = settingFs = parseInt(100 * (clientWidth / 750));
var whileCount = 0;
while (true) {
var realFs = parseInt(window.getComputedStyle(html).fontSize);
var delta = realFs - settedFs;
//不相等
if (Math.abs(delta) >= 1) {
if (delta > 0)
settingFs--;
else
settingFs++;
html.setAttribute('style', 'font-size:' + settingFs + 'px!important');
} else
break;
if (whileCount++ > 100)
break
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
//第二种
// (function(doc, win) {
// var docEl = doc.documentElement,
// // isIOS = navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
// dpr = Math.min(win.devicePixelRatio, 3),
// dpr = window.top === window.self ? dpr : 1, //被iframe引用时,禁止缩放
//
// // let dpr = win.devicePixelRatio,
// scale = 1 / dpr,
// resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
// docEl.dataset.dpr = dpr;
// var metaEl = doc.createElement('meta');
// metaEl.name = 'viewport';
// metaEl.content = 'initial-scale=' + scale + ',maximum-scale=' + scale + ', minimum-scale=' + scale;
// docEl.firstElementChild.appendChild(metaEl);
// var recalc = function() {
// var width = docEl.clientWidth;
// if (width / dpr > 750) {
// width = 750 * dpr;
// }
// // 乘以100,px : rem = 100 : 1
// docEl.style.fontSize = 100 * (width / 750) + 'px';
// };
// recalc()
// if (!doc.addEventListener) return;
// win.addEventListener(resizeEvt, recalc, false);
// })(document, window);