h5自适应js

// 768是最大相应尺寸
// 375是屏幕显示尺寸(如iphone6是375像素宽)
// 20是在375宽度下根标签的字体大小
(function () {
var supportsOrientationChange = 'onorientationchange' in window ? 'orientationchange' : 'resize';
var clientWidth = document.documentElement.clientWidth;
var timer;
var nowPX = 0;
function setRem() {
clientWidth = document.documentElement.clientWidth;
if(clientWidth >= 768){
nowPX = 768 / 375 * 20;
}else{
nowPX = clientWidth / 375 * 20;
}
document.documentElement.style.fontSize = nowPX + 'px';
}
clientWidth !== 375 && setRem();
window.addEventListener(supportsOrientationChange, function () {
clearTimeout(timer);
timer = setTimeout(function () {
setRem();
}, 300);
}, false);
})();

猜你喜欢

转载自www.cnblogs.com/My-Blogsphere/p/10298323.html