postcss-px2rem 安装以及使用

  1. 安装 :
    postcss-px2rem -d
    postcss-pxtorem -d
    px2rem-loader -d
  2. 在src 中 创建 utils 文件夹 创建 rem.js
    // 设置 rem 函数 比例为 1:100
function setRem() {
    //  PC端
    console.log('非移动设备')
    const baseSize = 100 // 基准值

    // 相对于1920像素的缩放比 
    let scale = document.documentElement.clientWidth / 1920
    // 根据屏幕变化 1rem 对应的 font-size
    scale = scale > 1 ? 1 : scale;
    const realFont = baseSize * scale
    document.documentElement.style.fontSize = realFont + 'px'

}
// 初始化
setRem();
// 改变窗口大小时重新设置 rem
window.onresize = function () {
    setRem()
};
  1. 新建 vue.config.js
  const px2rem = require('postcss-px2rem')
const postcss = px2rem({
    remUnit: 100 // 基准值
})

module.exports = {
    publicPath: './',
    css: {
        loaderOptions: {
            postcss: {
                plugins: [
                    postcss
                ]
            }
        }
    }
}

注意:
找到nodejs/llib-flexible/flexible.js 修改代码 如下:

 function refreshRem() {
        var width = docEl.getBoundingClientRect().width;
        if (width / dpr > 540) {
            // width = 540 * dpr;
            width = width * dpr; // 这行代码 pc适配的话 540改为width
        }
        var rem = width * dpr;
        docEl.style.fontSize = rem + 'px';
        flexible.rem = win.rem = rem;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值