js控制手機端字體大小rem


1 //得到手機屏幕的寬度
2 let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
3 if(htmlWidth>750){htmlWidth=750}
4 //得到html的Dom元素
5 let htmlDom = document.getElementsByTagName('html')[0];
6 //設置根元素字體大小
7 htmlDom.style.fontSize = htmlWidth/20 + 'px';
8 htmlDom.style.margin = '0 auto';
9 htmlDom.style.maxWidth = 750 + 'px';

比如說,手機寬度像素為360px的話,1rem就是18px;像素為320px的話,1rem就是16px.

 


免責聲明!

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



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