美文网首页
2018-12-29 vue-cli3.0中使用 postcss

2018-12-29 vue-cli3.0中使用 postcss

作者: luu | 来源:发表于2018-12-29 15:04 被阅读0次

1 、创建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()
}

2、在main.js中引入rem.js

import './utils/rem'
引入文件后,查看页面的html节点,是否有被自动添加 font-size。

注意:完成到这一步,也就是实现了rem布局,实际开发的时候,还是需要我们去计算对应的rem值去开发。

3 、配置 postcss-pxtorem 自动转换px为rem

1、安装 postcss-pxtorem

$ npm install postcss-pxtorem -D

2、在根目录下创建 vue.config.js配置postcss-pxtorem

module.exports = {
    lintOnSave: true,
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    require('postcss-pxtorem')({
                        rootValue : 32, // 换算的基数
                        selectorBlackList  : ['weui','mu'], // 忽略转换正则匹配项
                        propList   : ['*'],
                    }),
                ]
            }
        }
    },
}

按照上述配置项目后,即可在开发中直接使用 px 单位开发。

例如设计给出的设计图是 750 * 1136,那么可以直接在页面中写

body {
width: 750px;
height: 1136px;
}
将被转换为

body {
widht: 23.4375rem;
height: 35.5rem;
}

相关文章

网友评论

      本文标题:2018-12-29 vue-cli3.0中使用 postcss

      本文链接:https://www.haomeiwen.com/subject/hjzulqtx.html