vue项目中 使用 amfe-flexible(或lib-flexible) && postcss-pxtorem 自动将px转rem

1 amfe-flexible安装:npm i -S amfe-flexible
用于自动设置根节点字体大小

安装lib-flexible:npm install lib-flexible -S
lib-flexible已停止维护,可使用amfe-flexible代替
使用lib-flexible,修改最大540宽度限制
node_modules/lib-flexible/flexible.js 
注释掉这一项if判断
function refreshRem(){
    var width = docEl.getBoundingClientRect().width;
	/*
    if (width / dpr > 540) {
        width = 540 * dpr;
    }
	*/
    var rem = width / 10;
    docEl.style.fontSize = rem + 'px';
    flexible.rem = win.rem = rem;
}

2 在main.js中引入
import ‘amfe-flexible/index.min.js’
(或 import ‘lib-flexible’)

3 postcss-pxtorem安装:npm install postcss-pxtorem --save-dev
postcss-pxtorem是PostCSS的插件,用于将像素单元生成rem单位。

4 设置规则 更改postcss.config.js, 该文件为使用vue-cli3自动创建的文件
module.exports = {
plugins: {
‘autoprefixer’: {
browsers: [‘Android >= 4.0’, ‘iOS >= 7’]
},
‘postcss-pxtorem’: {
rootValue: 75, // 设计稿以宽度750px为例
selectorBlackList: [".van-"], //排除,van-开头的class,不进行rem转换
propList: [’*’]
}
}
}

/* style中,height: 500Px;  大写P开头的像素不进行转换 */
Logo

Vue社区为您提供最前沿的新闻资讯和知识内容

更多推荐