0
点赞
收藏
分享

微信扫一扫

vant移动端rem适配

乐百川 2022-01-25 阅读 82

如果需要使用 rem 单位进行适配,推荐使用以下两个工具:

  • postcss-pxtorem 是一款 PostCSS 插件,用于将 px 单位转化为 rem 单位
  • lib-flexible 用于设置 rem 基准值

使用 lib-flexible 动态设置 REM 基准值(html 标签的字体大小)

npm i amfe-flexible

然后在main.js导入(可打开页面看看html标签的font-size大小变化)

import 'amfe-flexible/index.min.js'

使用 postcss-pxtorem 将 px 转为 rem

安装 (建议使用指定版本安装)

 npm install postcss@8.4.5  postcss-pxtorem@5.1.1 --save-dev 

然后在根目录下创建postcss.config.js文件(针对postcss插件,适配设计稿的尺寸不是 375,而是 750 或其他大小,可以将 rootValue 配置调整为:)

// postcss.config.js
module.exports = {
  plugins: {
    // postcss-pxtorem 插件的版本需要 >= 5.0.0
    'postcss-pxtorem': {
      rootValue({ file }) {
        return file.indexOf('vant') !== -1 ? 37.5 : 75;
      },
      propList: ['*'],
    },
  },
};
举报

相关推荐

0 条评论