You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

postcss-px-to-vh

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-px-to-vh

A CSS post-processor that converts px to vh.

0.0.4
latest
npmnpm
Version published
Maintainers
1
Created
Source

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\//)
    • 如果值是一个正则表达式,将包含匹配的文件,否则将排除该文件
    • 如果传入的值是一个数组,那么数组里的值必须为正则

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

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
    }
  }
}

Keywords

css

FAQs

Package last updated on 14 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts