vue项目PC端适配 自适应

设计稿1980 * 1080

安装依赖

 "postcss-pxtorem": "^6.0.0"

vue.config.js 中配置

代码如下: 

css: {
    loaderOptions: {
      postcss: {
        postcssOptions:{
          plugins: [
            //
            require('postcss-pxtorem')({
              rootValue: 16,
              selectorBlackList: ['weui', 'mu'], // 忽略转换正则匹配项
              propList: ['*'],
            }),
          ]
        }
      }
    },
  }

utils中新建一个rem.js文件

代码如下:

// rem等比适配配置文件
// 基准大小
const baseSize = 16
// 设置 rem 函数
function setRem () {
  // 当前页面屏幕分辨率相对于 1920宽的缩放比例,可根据自己需要修改
  const scale = document.documentElement.clientWidth / 1920
  // 设置页面根节点字体大小(“Math.min(scale, 2)” 指最高放大比例为2,可根据实际业务需求调整)
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置 rem
window.onresize = function () {
  setRem()
}

<style lang="scss" scoped>

style 标签内可以正常按照px单位来写

但页面中要写成rem 单位

猜你喜欢

转载自blog.csdn.net/Lililiming_/article/details/128945117