vue 中使用rem布局

在使用vue-cli搭建好项目框架后,在目录结构的index.html文件中添加一段js代码:css


而后在写css就能够将px单位换成rem.
这里设置的比例是100px=1rem,
例如:宽度为100px时,能够直接写成1rem

=============================================================
2==vue cli3.0 rem 使用fnResize(); window.onresize = function () { fnResize(); } function fnResize() { var deviceWidth = document.documentElement.clientWidth || window.innerWidth; if (deviceWidth >= 750) { deviceWidth = 750; } if (deviceWidth <= 320) { deviceWidth = 320; } document.documentElement.style.fontSize = (deviceWidth / 7.5) + 'px'; }

vue cli3.0 rem 使用
首先安装amfe-flexible插件,在main.js里引入html

一、npm i amfe-flexible

二、import 'amfe-flexible'

而后再,安装postcss-px2rem插件vue

npm i postcss-px2rem

在package.json中配置vue-cli

"postcss": {
    "plugins": {
      "autoprefixer": {},
      "postcss-px2rem": {
        "remUnit": 37.5
      }
    }
  }

说明,我这里用的是vue-cli3.0脚手架。在.vue文件里。样式直接写px单位就能够了。在浏览器查看时会自动转换为rem单位。若是字体还想用px。那就这样将px大写。就不会编译为rem单位了。样式就能够实现px。npm

相关文章
相关标签/搜索