不借助第三方的插件,自己实现rem的功能封装。
- 手机尺寸<750px模拟器效果
- 最大尺寸750px效果
根html文件配置js
// 默认设计稿是750px,对应的字体大小为100px,1rem = 100px,方便我们开发中的计算,不用搞的很复杂,还要拿计算器那种就不用了。
(function () {
const computed = () => {
const deviceWidth = document.documentElement.clientWidth
const designWidth = 750
const ratio = deviceWidth*100/ designWidth
if(deviceWidth > designWidth) {
document.documentElement.style.fontSize = '100px'
}else{
document.documentElement.style.fontSize = ratio + 'px'
}
}
computed()
window.addEventListener('resize', computed, false)
})();
根html文件配置css
#root{
max-width: 750px;
margin:0 auto;
}