在模板中通过js操作进行适配
- 在生成页面时,会通过模板来生成,所以当我们在模板的head中进行js操作后,每次生成的页面都会进行字体的适配转换。
<html lang="en" style="font-size: 100px;">
<script>
(function(w){
var doc = w.document;
var docEl = doc.documentElement;
var timer;
function refersh() {
var width = docEl.getBoundingClientRect().width;
if(width>750){
width = 750;
}
var rem = width/7.5;
docEl.style.fontSize=rem+"px";
}
w.addEventListener("resize",function(){
timer && clearTimeout(timer);
timer = setTimeout(refersh,400);
})
w.addEventListener("pageshow",function(){
timer && clearTimeout(timer);
timer = setTimeout(refersh,400);
})
})(window)
</script>
使用媒体查询适配的方法
@media screen and (min-width:350px){
html{font-size:342%;}
}
@media screen and (min-width:360px){
html{font-size:351.56%;}
}
@media screen and (min-width:375px){
html{font-size:366.2%;}
}
@media screen and (min-width:384px){
html{font-size:375%;}
}
@media screen and (min-width:390px){
html{font-size:380.85%;}
}
@media screen and (min-width:393px){
html{font-size:383.79%;}
}
@media screen and (min-width:410px){
html{font-size:400%;}
}
@media screen and (min-width:432px){
html{font-size:421.875%;}
}
@media screen and (min-width:480px){
html{font-size:469%;}
}
@media screen and (min-width:540px){
html{font-size:527.34%;}
}
@media screen and (min-width:640px){
html{font-size: 625%;}
}
@media screen and (width:720px){
html{font-size: 703.125%;}
}