使用 rem
單位進(jìn)行適配权旷,使用以下兩個工具:
- postcss-pxtorem 是一款 PostCSS 插件,用于將 px 單位轉(zhuǎn)化為 rem 單位
- lib-flexible 用于設(shè)置 rem 基準(zhǔn)值
1危彩、安裝工具
npm install postcss-pxtorem lib-flexible
2、新建postcss.config.js泳桦,內(nèi)容配置如下
module.exports = {
plugins: {
'postcss-pxtorem': {
rootValue: 75, // 此處75是根據(jù)設(shè)計稿尺寸750設(shè)置汤徽,如果設(shè)計稿尺寸是375則改為37.5
propList: ['*'],
},
},
};
附錄:如果項(xiàng)目中使用了vant組件,并且你的項(xiàng)目的設(shè)計稿尺寸不是375灸撰,而是750或其他尺寸時谒府,由于vant組件是375尺寸的,所以要在postcss.config.js進(jìn)行區(qū)分
// postcss.config.js
module.exports = {
plugins: {
// postcss-pxtorem 插件的版本需要 >= 5.0.0
'postcss-pxtorem': {
rootValue({ file }) {
return file.indexOf('vant') !== -1 ? 37.5 : 75;
},
propList: ['*'],
},
},
};