vue项目实现pc端的屏幕适配

1.首先下载第三方插件

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

2.在根目录下面找到该文件

 3.更改flexible.js 中的源码

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

 又或者:根据自己的屏幕宽度的需求来调整if里面的判断

  function refreshRem() {
    var width = docEl.getBoundingClientRect().width;
    // if (width / dpr > 540) {
    //     width = 540 * dpr;
    // }
    if (width / dpr < 810) {
      width = 810 * dpr;
    }
    if (width / dpr < 1300) {
      width = 1300 * dpr;
    }
    if (width / dpr < 1920) {
      width = 1920 * dpr;
    }
      if (width / dpr < 2560) {
      width = 2560 * dpr;
    }
    var rem = width / 10;
    docEl.style.fontSize = rem + "px";
    flexible.rem = win.rem = rem;
  }

4.更改好源码以后,在全局文件main.js中引用

import 'lib-flexible'

5.配置插件(如果我们直接在vue.config.js中配置的话,重启项目会报一个 plugins的错误,所以我们可以在根目录下新建一个文件:postcss.config.js)

// // 引入等比适配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
  // 配置rem基准值 基准大小 baseSize
  remUnit: 192 // 基于1920的设计稿/10
})
module.exports = {
  chainWebpack: (config) => {
    config.module
      .rule('css')
      .test(/\.css$/)
      .oneOf('vue')
      .use('px2rem-loader')
      .loader('px2rem-loader')
      .options({
        remUnit: 192, // 基于设计稿的十分之一
        remPrecision: 8 // rem保留几位小数
      })
      .end()
  },
  css: {
    loaderOptions: {
      postcss: {
        plugins: [postcss]
      }
    }
  }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值