vue-vant使用postcss-pxtorem和amfe-flexible做移动端适配

vue-vant使用postcss-pxtorem和amfe-flexible做移动端适配


前言

手机端开发,市场上手机尺寸各种各样,加上还有paid,所以手机端的适配很重要,就这几天我自己遇到的适配问题,解决方案:postcss-pxtoremamfe-flexible配合使用,我先发一个纪录分享。

这次是h5页面开发,使用的是vue+vant构建项目,"vue": "^2.6.14","@vue/cli-plugin-babel": "~5.0.0","vant": "^2.12.51","amfe-flexible": "^2.2.1","postcss-pxtorem": "^6.0.0",


一、amfe-flexible、postcss-pxtorem是什么?

amfe-flexible:

        可以自动给 < html > 设置font-size值为设备宽度的1/10

postcss-pxtorem:

        rem方案虽然可以解决适配问题,但是如果UI设计稿使用的是px单位,自己转换成rem比较麻烦所以需要使用postcss-pxtorem,可以把px转换为rem

二、使用步骤

1.安装amfe-flexible

npm i amfe-flexible

2.配置:main.js

import 'amfe-flexible'

3.安装postcss-pxtorem

npm i postcss-pxtorem

 4.在项目根目录下新增配置文件postcss.config.js

module.exports = {
     plugins: {
         'postcss-pxtorem': {
             rootValue() {
                 return 75;
             },
             propList: ['*'],
         },
     },
 };

 5.兼容vant,设置postcss.config.js

        如果使用了vant组件库,vant的设计稿是根据375px去设计的,为了避免冲突需要单独设置

module.exports = {
     plugins: {
         'postcss-pxtorem': {
             rootValue({ file }) {
                 return file.indexOf('vant') !== -1 ? 37.5 : 75;
             },
             propList: ['*'],
         },
     },
 };


说在最后

如有不对之处或更好方法,感谢各位慷慨支招!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值