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的方式來設置才能成功設置,暫時不知道為什么。

留着以后解決



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM