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