vue使用rem实现窗口大小自适应

  1. 安装依赖
cnpm install lib-flexible --save
#需要指定5.1.1版本
cnpm install postcss-pxtorem@5.1.1 --save
  1. 引入插件(main.js里添加)
//自适应rem插件
import 'lib-flexible';
  1. 添加配置(脚手架2、3不一样)
  • cli3在vue.config.js中添加配置
module.exports = {
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          require('postcss-pxtorem')({ // 把px单位换算成rem单位
            rootValue: 37.5, //通常结合 lib-flexible 设置 rem 基准值,默认用37.5,不然容易出问题
            selectorBlackList: ['.ignore'],  //则是一个对css选择器进行过滤的数组,比如你设置为['fs'],那例如fs-xl类名,里面有关px的样式将不被转换
            propList: ['*']
          })
        ]
      }
    }
  }
};
  • cli2(根目录下.postcss.js文件(没有则新建一个))
module.exports = {
  plugins: {
    'autoprefixer': {
      overrideBrowserslist: [
        'Android >= 4.0',
        'iOS >= 7',
      ]
    },
    'postcss-pxtorem': {
      rootValue: 37.5, //通常结合 lib-flexible 设置 rem 基准值,默认用37.5,不然容易出问题
      selectorBlackList: ['.ignore'],  //则是一个对css选择器进行过滤的数组,比如你设置为['fs'],那例如fs-xl类名,里面有关px的样式将不被转换
      propList: ['*']
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值