npm install postcss postcss-pxtorem
"postcss": "^8.2.14",
"postcss-pxtorem": "^6.0.0"
}
新建rem.js
在main.js import './util/rem'
// 基准大小
const baseSize = 16
// 设置 rem 函数
function setRem () {
// 当前页面宽度相对于 1920宽的缩放比例,可根据自己需要修改。
let clientWidth = document.documentElement.clientWidth;
const scale = clientWidth / 1920
// 设置页面根节点字体大小(“Math.min(scale, 2)” 指最高放大比例为2,可根据实际业务需求调整)
document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置 rem
window.onresize = function () {
setRem()
}
根目录新建 postcss.config.js
plugins: {
autoprefixer: {
overrideBrowserslist: [
"Android 4.1",
"iOS 7.1",
"Chrome > 31",
"ff > 31",
"ie >= 8",
"last 10 versions", // 所有主流浏览器最近10版本用
],
grid: true,
},
'postcss-pxtorem': {
rootValue: 16,
propList: ['*'],
unitPrecision: 5
}
}
}