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.