zoukankan      html  css  js  c++  java
  • Vue项目中自动将px转换为rem

    一、配置与安装步骤:

     

    1、在 Vue 项目的 src 文件夹下创建一个 config 文件夹:

    2、在 config 文件夹中创建 rem.js:

    3、将以下代码复制到 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()
    }

    4、在 src 文件夹下的 main.js 中引入:

    import './config/rem'

    5、在 Vue 项目根目录终端引入:

    npm install postcss-pxtorem -D

    6、在 Vue 项目文件夹下的 postcss.config.js 中加入:

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

    至此,Vue 项目就能实现在页面中自动将 px 转换成  rem 了

    二、实例演示:

    假如给出设计图是 375*812,可以在代码中直接写入:

    div{
      width: 375px;
      height: 812px;
    }

    此时在页面中显示:

    如果要让部分属性不转换成 rem,可以将 px 写成 Px:

    div{
      width: 375Px;
      height: 812px;
    }

    这时在页面中就会保留 375px 了:

  • 相关阅读:
    Java 单测 回滚
    Java四种线程池的使用
    git gc
    Git 常用命令
    JPA 批量新增
    SpringData JPA 排除 扫描 exclude-filter 不能使用解决
    开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
    深入浅出 RPC
    阿里 Java面试 知识点
    Eclipse Egit 安装
  • 原文地址:https://www.cnblogs.com/Leophen/p/11283677.html
Copyright © 2011-2022 走看看