postcss-px-to-vh
v0.0.4
Published
A CSS post-processor that converts px to vh.
Downloads
10
Maintainers
Readme
postcss-px-to-vh
原项目 postcss-px-to-viewport
将px单位转换为视口单位的vh的插件.
简介
由于要做大屏项目,需要根据高度来动态调整缩放。但找不到px转vh的插件,只能拿大佬们的改改了。
安装
使用npm安装
$ npm install postcss-px-to-vh --save-dev
或者使用yarn进行安装
$ yarn add -D postcss-px-to-vh
配置参数
默认参数:
{
unitToConvert: 'px',
viewportHeight: 1080,
unitPrecision: 5,
propList: ['*'],
viewportUnit: 'vh',
fontViewportUnit: 'vh',
selectorBlackList: [],
minPixelValue: 1,
mediaQuery: false,
replace: true,
exclude: undefined,
include: undefined,
}
unitToConvert
(String) 需要转换的单位,默认为"px"viewportHeight
(Number) 设计稿的视口高度unitPrecision
(Number) 单位转换后保留的精度propList
(Array) 能转化为vh的属性列表- 传入特定的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\//
)- 如果值是一个正则表达式,将包含匹配的文件,否则将排除该文件
- 如果传入的值是一个数组,那么数组里的值必须为正则
exclude
和include
是可以一起设置的,将取两者规则的交集。
Ignoring (需要翻译帮助。)
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.
There are several more reasons why your pixels may not convert, the following options may affect this:
propList
, selectorBlackList
, minPixelValue
, mediaQuery
, exclude
, include
.
使用PostCss配置文件时
在postcss.config.js
添加如下配置
module.exports = {
plugins: {
// ...
'postcss-px-to-viewport': {
// options
}
}
}