You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.9 KiB

  1. # postcss-px-to-viewport
  2. [![NPM version](https://badge.fury.io/js/postcss-px-to-viewport.svg)](http://badge.fury.io/js/postcss-px-to-viewport)
  3. [English](README.md) | 中文
  4. 将px单位转换为视口单位的 (vw, vh, vmin, vmax) 的 [PostCSS](https://github.com/postcss/postcss) 插件.
  5. <a href="https://evrone.com/?utm_source=postcss-px-to-viewport">
  6. <img src="https://user-images.githubusercontent.com/417688/34437029-dbfe4ee6-ecab-11e7-9d80-2b274b4149b3.png"
  7. alt="Sponsored by Evrone" width="231">
  8. </a>
  9. ## 简介
  10. 如果你的样式需要做根据视口大小来调整宽度,这个脚本可以将你CSS中的px单位转化为vw,1vw等于1/100视口宽度。
  11. ### 输入
  12. ```css
  13. .class {
  14. margin: -10px .5vh;
  15. padding: 5vmin 9.5px 1px;
  16. border: 3px solid black;
  17. border-bottom-width: 1px;
  18. font-size: 14px;
  19. line-height: 20px;
  20. }
  21. .class2 {
  22. border: 1px solid black;
  23. margin-bottom: 1px;
  24. font-size: 20px;
  25. line-height: 30px;
  26. }
  27. @media (min-width: 750px) {
  28. .class3 {
  29. font-size: 16px;
  30. line-height: 22px;
  31. }
  32. }
  33. ```
  34. ### 输出
  35. ```css
  36. .class {
  37. margin: -3.125vw .5vh;
  38. padding: 5vmin 2.96875vw 1px;
  39. border: 0.9375vw solid black;
  40. border-bottom-width: 1px;
  41. font-size: 4.375vw;
  42. line-height: 6.25vw;
  43. }
  44. .class2 {
  45. border: 1px solid black;
  46. margin-bottom: 1px;
  47. font-size: 6.25vw;
  48. line-height: 9.375vw;
  49. }
  50. @media (min-width: 750px) {
  51. .class3 {
  52. font-size: 16px;
  53. line-height: 22px;
  54. }
  55. }
  56. ```
  57. ## 上手
  58. ### 安装
  59. 使用npm安装
  60. ```
  61. $ npm install postcss-px-to-viewport --save-dev
  62. ```
  63. 或者使用yarn进行安装
  64. ```
  65. $ yarn add -D postcss-px-to-viewport
  66. ```
  67. ### 配置参数
  68. 默认参数:
  69. ```js
  70. {
  71. unitToConvert: 'px',
  72. viewportWidth: 320,
  73. unitPrecision: 5,
  74. propList: ['*'],
  75. viewportUnit: 'vw',
  76. fontViewportUnit: 'vw',
  77. selectorBlackList: [],
  78. minPixelValue: 1,
  79. mediaQuery: false,
  80. replace: true,
  81. exclude: undefined,
  82. include: undefined,
  83. landscape: false,
  84. landscapeUnit: 'vw',
  85. landscapeWidth: 568
  86. }
  87. ```
  88. - `unitToConvert` (String) 需要转换的单位,默认为"px"
  89. - `viewportWidth` (Number) 设计稿的视口宽度
  90. - `unitPrecision` (Number) 单位转换后保留的精度
  91. - `propList` (Array) 能转化为vw的属性列表
  92. - 传入特定的CSS属性;
  93. - 可以传入通配符"*"去匹配所有属性,例如:['*'];
  94. - 在属性的前或后添加"*",可以匹配特定的属性. (例如['*position*'] 会匹配 background-position-y)
  95. - 在特定属性前加 "!",将不转换该属性的单位 . 例如: ['*', '!letter-spacing'],将不转换letter-spacing
  96. - "!" 和 "*"可以组合使用, 例如: ['*', '!font*'],将不转换font-size以及font-weight等属性
  97. - `viewportUnit` (String) 希望使用的视口单位
  98. - `fontViewportUnit` (String) 字体使用的视口单位
  99. - `selectorBlackList` (Array) 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
  100. - 如果传入的值为字符串的话,只要选择器中含有传入值就会被匹配
  101. - 例如 `selectorBlackList``['body']` 的话, 那么 `.body-class` 就会被忽略
  102. - 如果传入的值为正则表达式的话,那么就会依据CSS选择器是否匹配该正则
  103. - 例如 `selectorBlackList``[/^body$/]` , 那么 `body` 会被忽略,而 `.body` 不会
  104. - `minPixelValue` (Number) 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
  105. - `mediaQuery` (Boolean) 媒体查询里的单位是否需要转换单位
  106. - `replace` (Boolean) 是否直接更换属性值,而不添加备用属性
  107. - `exclude` (Array or Regexp) 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
  108. - 如果值是一个正则表达式,那么匹配这个正则的文件会被忽略
  109. - 如果传入的值是一个数组,那么数组里的值必须为正则
  110. - `include` (Array or Regexp) 如果设置了`include`,那将只有匹配到的文件才会被转换,例如只转换 'src/mobile' 下的文件
  111. - 如果值是一个正则表达式,将包含匹配的文件,否则将排除该文件
  112. - 如果传入的值是一个数组,那么数组里的值必须为正则
  113. - `landscape` (Boolean) 是否添加根据 `landscapeWidth` 生成的媒体查询条件 `@media (orientation: landscape)`
  114. - `landscapeUnit` (String) 横屏时使用的单位
  115. - `landscapeWidth` (Number) 横屏时使用的视口宽度
  116. > `exclude`和`include`是可以一起设置的,将取两者规则的交集。
  117. #### 直接在gulp中使用,添加gulp-postcss
  118. `gulpfile.js` 添加如下配置:
  119. ```js
  120. var gulp = require('gulp');
  121. var postcss = require('gulp-postcss');
  122. var pxtoviewport = require('postcss-px-to-viewport');
  123. gulp.task('css', function () {
  124. var processors = [
  125. pxtoviewport({
  126. viewportWidth: 320,
  127. viewportUnit: 'vmin'
  128. })
  129. ];
  130. return gulp.src(['build/css/**/*.css'])
  131. .pipe(postcss(processors))
  132. .pipe(gulp.dest('build/css'));
  133. });
  134. ```
  135. #### 使用PostCss配置文件时
  136. 在`postcss.config.js`添加如下配置
  137. ```js
  138. module.exports = {
  139. plugins: {
  140. // ...
  141. 'postcss-px-to-viewport': {
  142. // options
  143. }
  144. }
  145. }
  146. ```
  147. ## 测试
  148. 为了跑测试案例,您需要安装开发套件:
  149. ```
  150. $ npm install
  151. ```
  152. 然后输入下面的命令:
  153. ```
  154. $ npm run test
  155. ```
  156. ## 参与贡献
  157. 在提PR之前,请先阅读 [代码指南](CODE-OF-CONDUCT.md) 和 [贡献指南](CONTRIBUTING.md)
  158. ## 版本跟踪
  159. 使用 [SemVer](http://semver.org/) 做版本跟踪, 可用版本可在[这](https://github.com/evrone/postcss-px-to-viewport/tags)看到
  160. ## Changelog
  161. 变更日志在 [](CHANGELOG.md).
  162. ## 作者
  163. * [Dmitry Karpunin](https://github.com/KODerFunk) - *Initial work*
  164. * [Ivan Bunin](https://github.com/chernobelenkiy)
  165. 在 [contributors](https://github.com/evrone/postcss-px-to-viewport/contributors) 里可以看到谁参与了本项目.
  166. ## 许可
  167. 本项目使用 [MIT License](LICENSE).
  168. ## 借鉴自
  169. * 受 https://github.com/cuth/postcss-pxtorem/ 启发有了这个项目