let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth; 获取屏幕宽度
let htmlDom=document.getElementsByTagName('html')[0]; 获取html
htmlDom.style.fontSize=htmlWidth / 10 + 'px'; 让font-size随屏幕宽度改变 从而影响rem
window.addEventListener('resize',()=>{ 在窗口大小改变的时候同时改变fontsize
let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth; 重新获取屏幕宽度
let htmlDom=document.getElementsByTagName('html')[0];
htmlDom.style.fontSize=htmlWidth / 10 + 'px';
})
scss中编写方法
@function pxToRem($px) {
$rem:37.5px; html的字体大小为width / 10 所以页面需要在宽度为375的模拟机上编写
@return ($px / $rem)+rem;
}