vue 移動端屏幕適配 使用rem

要想移動端適配 並使用 rem  您須要先看這篇文章,配置好less ➡️ 在vue 中使用 less,就能夠使用rem了css

 

若是項目已經開發的差很少了,沒有用到rem 又要使用rem,您用這招。html

postcss-pxtorem:轉換px爲rem的插件vue

安裝 postcss-pxtorem

npm install postcss-pxtorem --save

 

新建rem.js文件

const baseSize = 32
// 設置 rem 函數
function setRem () {
  // 當前頁面寬度相對於 750 寬的縮放比例,可根據本身須要修改。
  const scale = document.documentElement.clientWidth / 750
  // 設置頁面根節點字體大小
  document.documentElement.style.fontSize = (baseSize * Math.min(scale, 2)) + 'px'
}
// 初始化
setRem()
// 改變窗口大小時從新設置 rem
window.onresize = function () {
  setRem()
}

並引用進main.js文件內npm

import './rem'

 

 修改.postcssrc.js 文件

.postcssrc.js文件內的 plugins 添加如下配置,配後就能夠在開發中直接使用 px 單位開發了less

    "postcss-pxtorem": {
      "rootValue": 32,
      "propList": ["*"]
    }

 

helloworld.vue函數

<template>
  <div class="hello">
    test
  </div>
</template>

<script>
  export default {
    name: 'HelloWorld',
    data() {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    }
  }
</script>

<style scoped>
  .hello {
    text-align: center;
    font-size: 20px;
    width: 300px;
    height: 400px;
    background:red;
  }
</style>

效果post

 

 

此隨筆乃本人學習工做記錄,若有疑問歡迎在下面評論,轉載請標明出處。學習

若是對您有幫助請動動鼠標右下方給我來個贊,您的支持是我最大的動力字體

相關文章
相關標籤/搜索