Home > Web Front-end > JS Tutorial > How vue uses rem for mobile screen adaptation (with code)

How vue uses rem for mobile screen adaptation (with code)

不言
Release: 2018-09-27 15:35:18
Original
8556 people have browsed it

The content of this article is about how vue uses rem for mobile screen adaptation (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If you want to adapt and use rem on the mobile terminal, you need to read this article first. After configuring less, you can use rem

If the project has been almost developed, it is useless. To use rem again, you use this trick.

postcss-pxtorem: Plug-in for converting px to rem

Installation postcss-pxtorem

npm install postcss-pxtorem --save
Copy after login

Newrem .js file

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()
}
Copy after login

and reference it into the main.js file

import './rem'
Copy after login

Modification .postcssrc.js file

Add the following configuration to the plugins in the .postcssrc.js file. After configuration, you can use the px unit directly during development. Developed

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

helloworld.vue

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

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

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

effect

The above is the detailed content of How vue uses rem for mobile screen adaptation (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template