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.

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