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