vue react PC/移动端 lib-flexible postcss-px2rem 一套自适应方法

安装 lib-flexible postcss-px2rem 两个包

npm i lib-flexible --save
npm i postcss-px2rem --save

vue 在main.js 中引入,react 在index.js 中引入

import 'lib-flexible'

modul文件夹中找到 lib-flexible ->flexible.js

  function refreshRem () {
    var width = docEl.getBoundingClientRect().width;
    if (width / dpr > 540) {
      width = 540 * dpr; //将540 改为width!!!!
    }
    var rem = width / 10;
    docEl.style.fontSize = rem + 'px';
    flexible.rem = win.rem = rem;
  }

react 在config文件夹下的webpack.config.dev.js文件中加入
在这里插入图片描述
在这里插入图片描述
按照比例来UI是1920就 /10 就是192

vue中 在根目录下新建 vue.config.js

 module.exports = {
    css: {
        loaderOptions: {
          postcss: {
            // 这是rem适配的配置  注意: remUnit在这里要根据lib-flexible的规则来配制,如果您的设计稿是750px的,用75就刚刚好。
             plugins: [
              require("postcss-px2rem")({
                remUnit: 192
          })
        ]
      }
    }
 }

就可以实现PC,移动两端都适配了

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值