VUE:移动端rem适配

适用于H5 移动端字体px自动转换为rem

说明:

  • 包含两种方法

方法1

  • 插件下载
cnpm install postcss-pxtorem --save
  • 新建rem.js文件,然后引入main.js中

const baseSize = 32
// 设置 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()
}
  • 新建postcssrc.js文件

module.exports = {
  plugins: {
    "postcss-pxtorem": {
      "rootValue": 32,
      "propList": ["*"]
    },
  }
}

方法2:

  • postcss-pxtorem用于将单位转化为rem
npm install postcss-pxtorem --save-dev
  • lib-flexible 用于设置rem基准值
npm i -S amfe-flexible
  • main.js 引入amfe-flexible
import 'amfe-flexible/index.js'
  • 在package.json文件代码末尾加入以下代码
 "postcss": {
    "plugins": {
      "autoprefixer": {
        "overrideBrowserslist": [
          "Android >= 4.0",
          "iOS >= 7",
          "Chrome > 31",
          "ff > 31",
          "ie >= 8"
        ]
      },
      "postcss-pxtorem": {
        "rootValue": 37.5,
        "propList": [
          "*"
        ]
      }
    }
  }
posted @ 2021-04-19 14:00  四驱兄嘚  阅读(177)  评论(0)    收藏  举报