在 vue 项目中使用 postcss-px-to-viewport 做移动端适配

本文介绍了如何在Vue项目中通过npm安装并配置autoprefixer处理浏览器前缀,以及使用postcss-px-to-viewport进行像素单位转换,重点讲解了在vue.config.js中的配置,包括exclude和include选项的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载 autoprefixer 用来给不同的浏览器自动添加相应前缀,如-webkit-,-moz-等等

npm i autoprefixer

安装 postcss-px-to-viewport

npm i postcss-px-to-viewport

根据是否需要使用include,引用不同的资源,不用github版本资源时,include属性设置无效

npm i https://github.com/evrone/postcss-px-to-viewport --save-dev

在 vue.config.js 文件里:

const pxtoviewport = require('postcss-px-to-viewport')
const autoprefixer = require('autoprefixer')

module.exports = {
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          pxtoviewport({
            unitToConvert: 'px',
            viewportWidth: 750,
            unitPrecision: 3,
            propList: [
              '*'
            ],
            viewportUnit: 'vw',
            fontViewportUnit: 'vw',
            selectorBlackList: [],
            minPixelValue: 1,
            mediaQuery: false,
            replace: true,
            exclude: /[\\/]node_modules[\\/]/,
            include: /[\\/]src[\\/]views[\\/]modulesApp[\\/]/
          })
        ]
      }
    }
  },
}

解释:

设定750px转化为100vw

exclude: /[\\/]node_modules[\\/]/ :排除node_modules下的所有文件

include: /[\\/]src[\\/]views[\\/]modulesApp[\\/]/ :给modulesApp下的所有文件做适配

如果使用了include配置项,就不需要使用exclude配置项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

subsistent

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值