h5中封装rem实现内容的响应式

不借助第三方的插件,自己实现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;
}

猜你喜欢

转载自blog.csdn.net/qq_27702739/article/details/143335636