document.documentelement.style.fontsize无法设置的问题

(function(){
'use strict';

let doc=document.documentElement;//减少dom操作
function resize(){
//获取dom文档宽
let clientWidth= window.innerWidth || doc.getBoundingClientRect().width ;
let fontSize = doc.style.fontSize;
//动态改变html的font-size,以320为例
if(clientWidth<375){//设置边界值以防万一
fontSize = '100px';
}else{
fontSize = clientWidth/375*100 + 'px';
}
}
//检测屏幕尺寸变化同步font-size,如横竖屏切换时触发
window.onresize = function(){
resize()
};
//页面初始化时触发
resize()
})();

提前获取fontsize存储到变量中,却无法成功设置html的fontsize,但是fontsize这个变量是获取到了结果的,在网页上却没有成功。只有在if-else代码块中通过doc.style.fontsize的方式来设置才能成功设置,暂时不知道为什么。

留着以后解决


猜你喜欢

转载自www.cnblogs.com/lqywong/p/11653681.html
今日推荐