Vue适配移动端--postcss-pxtorem转px为rem

2020-01-17  本文已影响0人  二营长家的张大炮

今天写代码的的时候,就想着适配一下移动端样式
然后就去找了点资料:
通过安装依赖以及修改配置使px转为rem适配移动端大小

安装依赖

npm i postcss-pxtorem

配置vue.config.js

  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          require('postcss-pxtorem')({
            rootValue: 16, // 换算的基数
            selectorBlackList: [], // 忽略转换正则匹配项
            propList: ['*'],
          }),
        ]
      }
    }
  }

创建rem.ts

// 基准大小
const baseSize = 10
//  设置 rem 函数
function setRem() {
    //   当前页面宽度相对于 750 宽的缩放比例,可根据自己需要修改。
    const scale = document.documentElement.clientWidth / 750
    //  设置页面根节点字体大小
    document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px'
}
// 初始化
setRem()
//  改变窗口大小时重新设置 rem
window.onresize = function () {
    setRem()
}

在入口文件中引入rem.ts

上一篇下一篇

猜你喜欢

热点阅读