移动端适配(rem)

2019-08-28  本文已影响0人  好名字都让你们用了

手机的分辨率不同,如何在不同的手机上保持显示相同的表现,常见的方案有两种:

1: 使用响应式布局,

(1):通过媒体 查询@media来实现

2:使用rem

3:在项目中配置rem

第一种方法:

在html的头部加上以下代码,元素单位全部用rem来表示
<script>
        function resetWidth() {
            // 兼容ie浏览器 document.body.clientWidth
            var baseWidth = document.documentElement.clientWidth ||                     document.body.clientWidth;
            console.log(baseWidth);
            // 设置跟标签字体大小
            document.documentElement.style.fontSize = baseWidth * 100 / 375 + 'px'
        }
        resetWidth();
        window.addEventListener('resize', function () {
            resetWidth();
        })
    </script>
    
     <style>
    html {
      font-size: 100px;
    }

    body {
      font-size: 16px;
    }
  </style>

第二种方法:

  1. 安装amfe-flexible,作用是根据手机大小设置根标签字体

    npm i amfe-flexible --save-dev
    
  2. main.js中导入

    import 'amfe-flexible'
    
  3. 安装postcss-pxtorem // 把px编程rem
    less,scc(预处理语言) => css => postcss(后处理语言)

    npm i postcss-pxtorem --save-dev
    
  4. 在vue-cli3项目根目录创建vue.config.js,添加以下代码

    const pxtorem = require("postcss-pxtorem");
    module.exports = {
      css: {
        loaderOptions: {
          postcss: {
            plugins: [
              //  autoprefixer(),
              pxtorem({
                rootValue: 37.5,
                propList: ["*"]
              })
            ]
          }
        }
      }
    };
    
上一篇 下一篇

猜你喜欢

热点阅读