rem布局的原理就是获取手机屏幕宽度,将屏幕宽度平均分配成多少份,每一份的宽度将其赋值给最大的document的fontsize。
resize();
window.onresize = function () {
resize();
}
function resize() {
var deviceWidth = document.documentElement.clientWidth || window.innerWidth;
if (deviceWidth >= 750) {
deviceWidth = 750;
document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px';
}
// 这里你可以根据你的设计图的宽度进行调节。如果是750宽的设计图就可以用上面的if判断,如果是32偶读就用下面的。然后根据设计图进行一比一的还原就可以了。1rem===100px
if (deviceWidth <= 320) {
deviceWidth = 320;
document.documentElement.style.fontSize = (deviceWidth / 3.2) + 'px';
}
}
.divWidth:{
width:1.5rem;//相当于设计图上150px
}