rem適配程式碼 直接放進頭部即可
<script>
(function () {
var timer;
changeFixedW();
function changeFixedW() {
var width = window.screen.width;
var fixedW = 375;
var scale = width / fixedW;
var meta = document.createElement('meta');
meta.setAttribute('name','viewport');
meta.setAttribute('content','width='+fixedW+',initial-scale='+scale+',maximum-scale='+scale+',user-scalable=no');
document.head.appendChild(meta);
}
window.addEventListener('resize', function () {
clearTimeout(timer);
timer = setTimeout(changeFixedW, 300);
}, false);
window.addEventListener('pageshow', function (e) {
if (e.persisted) {
clearTimeout(timer);
timer = setTimeout(changeFixedW, 300);
}
}, false);
})();
</script>
方法二
<script type="text/javascript" charset="utf-8">
(function () {
var timer;
changeFixedW();
function changeFixedW(){
var html = document.querySelector('html');
var width = html.getBoundingClientRect().width;
html.style.fontSize = width / 10 + 'px';
}
window.addEventListener('resize', function () {
clearTimeout(timer);
timer = setTimeout(changeFixedW, 300);
}, false);
})();
</script>