vue-移动端适配-postcss-pxtorem

本文介绍了如何在Vue项目中实现移动端适配,通过lib-flexible设置基础字体大小,然后利用postcss-pxtorem自动转换px到rem。配置包括安装依赖、在main.js中引入、设置.postcssrc.js文件,并针对Vant组件进行特殊处理,以及忽略特定文件。同时,为解决iPad设备适配问题,在index.html中添加了特殊代码。

vue-移动端适配-postcss-pxtorem

  1. 安装lib-flexible
npm i lib-flexible --save
  1. 在main.js引入lib-flexible
import 'lib-flexible/flexible'
  1. 安装postcss-pxtorem
npm install postcss-pxtorem@5.1.1 -D
  1. 配置postcss-pxtorem
    在根目录的.postcssrc.js文件中修改
    此文件自动生成,若没有,手动添加
module.exports = {
  "plugins": {
    "autoprefixer": {},
    'postcss-pxtorem': {
      rootValue: 37.5, // 75表示750设计稿,37.5表示375设计稿
      propList: ['*']
    }
  }
}
  1. 对于引入vant组件的特殊处理
  • vant设计稿为375,当以750设计稿为基础设置时,需要屏蔽vant
在postcss-pxtorem对象中添加
selectorBlackList: ['van']
  • 忽略文件
在postcss-pxtorem对象中添加
exclude: /web/i     //忽略 web下所有文件
  1. 针对ipad 和 ipad pro 设备无效,针对这些设备网上找到了方法,记录下
// 在index.html中添加如下代码
<script>
    /(pad|pod|iPad|iPod|iOS)/i.test(navigator.userAgent)&&(head=document.getElementsByTagName('head'),viewport=document.createElement('meta'),viewport.name='viewport',viewport.content='target-densitydpi=device-dpi, width=480px, user-scalable=no',head.length>0&&head[head.length-1].appendChild(viewport));
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值