function pixel2rem() {
var docEl = document.documentElement || document.body,
isIOS = navigator.userAgent.match(/iphone|ipod|ipad/gi),
dRatio = window.devicePixelRatio,
dpr = isIOS && dRatio != undefined ? Math.min(dRatio, 3) : (dRatio != undefined ? dRatio : 1), //设备像素比devicePixelRatio
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
docEl.dataset.dpr = dpr;
var recalc = function() {
//页面内容可见区域的宽度(兼容各种平台),对于ios设备devicePixelRatio值只可能是1或者2
//当width=device-width时,可视区的宽度就是document.documentElement.clientWidth
//安卓平台下可见区的宽度:document.body.clientWidth || document.documentElement.clientWidth
var width = docEl.clientWidth || window.innerWidth;
if (width / dpr > 750) {
width = 750 * dpr;
}
docEl.style.fontSize = 100 * (width / 750) + 'px';
};
recalc();
if (!document.addEventListener) return;
window.addEventListener(resizeEvt, recalc, false);
}
export default pixel2rem
在main.js中引用