关于vue.js:vue使用rem适配

7次阅读

共计 921 个字符,预计需要花费 3 分钟才能阅读完成。

1. 开发环境 vue
2. 电脑系统 windows10 专业版
3. 在应用 vue 开发挪动端的过程中, 咱们会因为兼容性而头疼, 上面我来分享分享上面 vue 应用 rem 自适配, 心愿对你有所帮忙。
4. 废话不多说, 间接上操作:

// 装置 postcss-pxtorem
npm i postcss-pxtorem -S

5. 在 src 目录新建 rem 文件夹, 上面新建 rem.js, 增加如下代码:

// 基准大小
const baseSize = 37.5
// 设置 rem 函数
function setRem() {
 const salepro = document.documentElement.clientWidth / 750
 // 以后页面宽度绝对于 750 宽的缩放比例,可依据本人须要批改.
 // 设置页面根节点字体大小
 document.documentElement.style.fontSize = (baseSize * Math.min(salepro, 2)) + 'px'
}
// 初始化
setRem()
// 扭转窗口大小时从新设置 rem
window.onresize = function () {setRem()
}

6. 在我的项目根目录新建 .postcssrc.js, 增加代码如下:

module.exports = {
 "plugins": {
  "postcss-pxtorem": {
   "rootValue": 37.5,
   "propList": ["*"]
  }
 }
}

留神: 我在配置中, 比例是 1:1, 也就是设计图宽是 750px, 你在 css 中间接写 width:750px; 就能够啦, 不必进行换算, 是不是很棒。

7. 在 main.js 中引入

import '@/rem/rem.js'

8. 在 vue 模板中应用,css 中增加如下代码:

<style lang="scss" scoped>
.about {
  width: 750px;
  height: 100vh;
  box-sizing: border-box;
  background-color: blue !important;
  .kk {
    width: 350px;
    height: 350px;
    background-color: red;
  }
}
</style>

9. 效果图如下:

10. 本期的分享到了这里就完结啦, 心愿对你有所帮忙, 让咱们一起致力走向巅峰。

正文完
 0