首先,要添加如下代码。才可以正常用REM来处理各不同大小的手机屏
function resize() {
/*if (window.orientation === 180 || window.orientation === 0) {
alert('竖屏状态!');
var screenW = window.screen.width;
}
if (window.orientation === 90 || window.orientation === -90 ){
alert('横屏状态!');
var screenW = window.screen.width;
}*/
var screenW;
if (!!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
screenW = window.innerWidth;
} else {
//alert('b');
screenW = window.screen.width;
}
//var screenW = window.screen.width;//window.outerWidth == undefined ? window.innerWidth : window.outerWidth;//window.screen.width;
var scale = 7.5;
var fontsize = (screenW / scale).toFixed(2);
window.ComputedFontSize = (100 / fontsize).toFixed(2);
document.documentElement.style.fontSize = fontsize + 'px';
}
resize();
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", function() {
resize();
}, false);
这段代码,一开始不等DOM加载完,就需要执行。
然后100PX正好是1rem,根据这个,各大小都好弄了。
但是有一个要注意,如果你的边线只有1px 就不要用REM了,因为有时候他在小的屏幕上,就不显示边线了。为了防止出现这个情况。比较小的值,还是用PX吧。