美文网首页
vue-cli3配置rem

vue-cli3配置rem

作者: 小李不小 | 来源:发表于2021-06-15 13:50 被阅读0次

1.安装
cnpm i postcss-pxtorem --save

2.项目根目录下新建 postcss.config.js 文件
postcss.config.js 内容:


module.exports = {
  plugins: {
    autoprefixer: {
      browsers: ['Android >= 4.0', 'iOS >= 7'],
    },
    'postcss-pxtorem': {
      rootValue: 16,
      propList: ['*'],
    },
  },
};
  1. 在main.js中引入如下 rem.js 文件

/* eslint-disable */
// 基准大小
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()

}

相关文章

网友评论

      本文标题:vue-cli3配置rem

      本文链接:https://www.haomeiwen.com/subject/ehmejltx.html