postcss-pxtorem 学习与使用

本文介绍了在前端开发中如何进行移动端适配,包括安装lib-flexible和postcss-pxtorem库,以及它们的配置方法。通过在main.js中引入lib-flexible并设置.postcssrc.js或postcss.config.js文件来转换px到rem。同时,文章提到了针对Vant组件库的特殊处理和解决iPad设备适配问题的方案。对于遇到'PostCSS plugin postcss-pxtorem requires PostCSS 8.'错误的情况,建议指定postcss-pxtorem版本为5.1.1。
  1. 安装lib-flexible 和 postcss-pxtorem

安装

npm install lib-flexible --save

yarn add lib-flexible --save
npm install postcss postcss-pxtorem --save-dev

yarn add postcss postcss-pxtorem --save-dev
  1. 在main.js 中引入 lib-flexible
import 'lib-flexible/flexible'
  1. 配置postcss-pxtorem
    在根目录的.postcssrc.js或者postcss.config.js文件中修改
    此文件自动生成,若没有,手动添加(创建上面两个文件其中一个就可以)
module.exports = {
  "plugins": {
    "autoprefixer": {},
    'postcss-pxtorem': {
      rootValue: 75, // 75表示750设计稿,37.5表示375设计稿
      propList: ['*']//(数组)可以从 px 更改为 rem 的属性  使用通配符*启用所有属性。例子:['*']
    }
  }
}
  1. options
    Type: Object | Null
    Default:
{
    rootValue: 16,
    unitPrecision: 5,
    propList: ['font', 'font-size', 'line-height', 'letter-spacing'],
    selectorBlackList: [],
    replace: true,
    mediaQuery: false,
    minPixelValue: 0,
    exclude: /node_modules/i
}
  • rootValue (Number | Function) Represents the root element font size or returns the root element font size based on the input parameter
  • unitPrecision (Number) The decimal numbers to allow the REM units to grow to.
  • propList (Array) The properties that can change from px to rem.
    • Values need to be exact matches.
    • Use wildcard * to enable all properties. Example: [’*’]
    • Use * at the start or end of a word. ([‘position’] will match background-position-y)
    • Use ! to not match a property. Example: [’*’, ‘!letter-spacing’]
    • Combine the “not” prefix with the other prefixes. Example: [’’, '!font’]
  • selectorBlackList (Array) The selectors to ignore and leave as px.
    • If value is string, it checks to see if selector contains the string.
      • [‘body’] will match .body-class
  • If value is regexp, it checks to see if the selector matches the regexp.
    • [/^body$/] will match body but not .body
  • replace (Boolean) Replaces rules containing rems instead of adding fallbacks.
  • mediaQuery (Boolean) Allow px to be converted in media queries.
  • minPixelValue (Number) Set the minimum pixel value to replace.
  • exclude (String, Regexp, Function) The file path to ignore and leave as px.
    • If value is string, it checks to see if file path contains the string.
      • ‘exclude’ will match \project\postcss-pxtorem\exclude\path
    • If value is regexp, it checks to see if file path matches the regexp.
      • /exclude/i will match \project\postcss-pxtorem\exclude\path
    • If value is function, you can use exclude function to return a true and the file will be ignored.
      • the callback will pass the file path as a parameter, it should returns a Boolean result.
      • function (file) { return file.indexOf(‘exclude’) !== -1; }
  1. 关于忽略属性的消息
    目前,忽略单个属性的最简单方法是在像素单位声明中使用大写字母。
// `px` is converted to `rem`
.convert {
    font-size: 16px; // converted to 1rem
}

// `Px` or `PX` is ignored by `postcss-pxtorem` but still accepted by browsers
.ignore {
    border: 1Px solid; // ignored
    border-width: 2PX; // ignored
}
  1. 对于引入vant组件的特殊处理
  • vant设计稿为375,当以750设计稿为基础设置时,需要屏蔽vant
  • 忽略文件
在postcss-pxtorem对象中添加
exclude: /web/i     //忽略 web下所有文件

针对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>
  1. 补充
    若运行报错 “ Error: PostCSS plugin postcss-pxtorem requires PostCSS 8.”
    直接指定安装postcss-pxtorem版本为5.1.1,就不会报这个错了
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值