第一步:
在vue项目中的src文件夹下面创建一个config文件夹,在里面创建一个rem.js文件
第二步:将下面代码复制到rem.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()
}
第三步:在 src 文件夹下的 main.js 中引入
import './config/rem'
第四步:在 Vue 项目根目录终端引入
下载:npm install postcss-pxtorem -D
第五步:在 Vue 项目文件夹下的 postcss.config.js 中加入
module.exports = {
plugins: {
autoprefixer: {},
"postcss-pxtorem": {
"rootValue": 16,
"propList": ["*"]
}
}
}
这样vue项目就可以自动转换成rem了!!!!