如何在nuxt3中使用postcss-px-to-viewport插件

在nuxt中使用vant-ui

在nuxt中使用postcss-px-to-viewport

1.安装vant模块

# 通过 npm 安装
npm i @vant/nuxt -D

# 通过 yarn 安装
yarn add @vant/nuxt -D

# 通过 pnpm 安装
pnpm add @vant/nuxt -D

# 通过 Bun 安装
bun add @vant/nuxt -D

2.增加vant模块

nuxt.config.ts 文件中增加模块:

export default defineNuxtConfig({
  modules: ['@vant/nuxt'],
});

3.安装postcss-px-to-viewport模块

使用npm安装

$ npm install postcss-px-to-viewport --save-dev

或者使用yarn进行安装

$ yarn add -D postcss-px-to-viewport

4.增加postcss-px-to-viewport模块

nuxt.config.ts 文件中增加模块:

export default defineNuxtConfig({
  postcss: {
    plugins: {
      'postcss-px-to-viewport': {
        viewportWidth: 375,
      }
    }
  }
})
配置参数

默认参数:

{
  unitToConvert: 'px',
  viewportWidth: 320,
  unitPrecision: 5,
  propList: ['*'],
  viewportUnit: 'vw',
  fontViewportUnit: 'vw',
  selectorBlackList: [],
  minPixelValue: 1,
  mediaQuery: false,
  replace: true,
  exclude: undefined,
  include: undefined,
  landscape: false,
  landscapeUnit: 'vw',
  landscapeWidth: 568
}
  • unitToConvert (String) 需要转换的单位,默认为"px"
  • viewportWidth (Number) 设计稿的视口宽度
  • unitPrecision (Number) 单位转换后保留的精度
  • propList (Array) 能转化为vw的属性列表
    • 传入特定的CSS属性;
    • 可以传入通配符""去匹配所有属性,例如:[''];
    • 在属性的前或后添加"*",可以匹配特定的属性. (例如['position'] 会匹配 background-position-y)
    • 在特定属性前加 "!",将不转换该属性的单位 . 例如: ['*', '!letter-spacing'],将不转换letter-spacing
    • "!" 和 ""可以组合使用, 例如: ['', '!font*'],将不转换font-size以及font-weight等属性
  • viewportUnit (String) 希望使用的视口单位
  • fontViewportUnit (String) 字体使用的视口单位
  • selectorBlackList (Array) 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
    • 如果传入的值为字符串的话,只要选择器中含有传入值就会被匹配
      • 例如 selectorBlackList['body'] 的话, 那么 .body-class 就会被忽略
    • 如果传入的值为正则表达式的话,那么就会依据CSS选择器是否匹配该正则
      • 例如 selectorBlackList[/^body$/] , 那么 body 会被忽略,而 .body 不会
  • minPixelValue (Number) 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
  • mediaQuery (Boolean) 媒体查询里的单位是否需要转换单位
  • replace (Boolean) 是否直接更换属性值,而不添加备用属性
  • exclude (Array or Regexp) 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
    • 如果值是一个正则表达式,那么匹配这个正则的文件会被忽略
    • 如果传入的值是一个数组,那么数组里的值必须为正则
  • include (Array or Regexp) 如果设置了include,那将只有匹配到的文件才会被转换,例如只转换 'src/mobile' 下的文件 (include: /\/src\/mobile\//)
    • 如果值是一个正则表达式,将包含匹配的文件,否则将排除该文件
    • 如果传入的值是一个数组,那么数组里的值必须为正则
  • landscape (Boolean) 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
  • landscapeUnit (String) 横屏时使用的单位
  • landscapeWidth (Number) 横屏时使用的视口宽度

excludeinclude是可以一起设置的,将取两者规则的交集。

忽略某些px值

You can use special comments for ignore conversion of single lines:

  • /* px-to-viewport-ignore-next */ — on a separate line, prevents conversion on the next line.
  • /* px-to-viewport-ignore */ — after the property on the right, prevents conversion on the same line.

Example:

/* example input: */
.class {
  /* px-to-viewport-ignore-next */
  width: 10px;
  padding: 10px;
  height: 10px; /* px-to-viewport-ignore */
  border: solid 2px #000; /* px-to-viewport-ignore */
}

/* example output: */
.class {
  width: 10px;
  padding: 3.125vw;
  height: 10px;
  border: solid 2px #000;
}

There are several more reasons why your pixels may not convert, the following options may affect this: propList, selectorBlackList, minPixelValue, mediaQuery, exclude, include.

参考1:Vant 4 - A lightweight, customizable Vue UI library for mobile web apps.

参考2:https://github.com/evrone/postcss-px-to-viewport/blob/master/README_CN.md

  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NCDS程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值