title: 设计稿及尺寸单位

在 Taro 中尺寸单位建议使用 px百分比 %,Taro 默认会对所有单位进行转换。在 Taro 中书写尺寸按照 1:1 的关系来进行书写,即从设计稿上量的长度 100px,那么尺寸书写就是 100px,当转成微信小程序的时候,尺寸将默认转换为 100rpx,当转成 H5 时将默认转换为以 rem 为单位的值。

如果你希望部分 px 单位不被转换成 rpx 或者 rem ,最简单的做法就是在 px 单位中增加一个大写字母,例如 Px 或者 PX 这样,则会被转换插件忽略。

结合过往的开发经验,Taro 默认以 750px 作为换算尺寸标准,如果设计稿不是以 750px 为标准,则需要在项目配置 config/index.js 中进行设置,例如设计稿尺寸是 640px,则需要修改项目配置 config/index.js 中的 designWidth 配置为 640

```jsx title=”/config/index.js” const config = { projectName: ‘myProject’, date: ‘2018-4-18’, designWidth: 640, …. }

  1. 目前 Taro 支持 `750` `640` `828` 三种尺寸设计稿,他们的换算规则如下:
  2. ```jsx
  3. const DEVICE_RATIO = {
  4. '640': 2.34 / 2,
  5. '750': 1,
  6. '828': 1.81 / 2
  7. }

建议使用 Taro 时,设计稿以 iPhone 6 750px 作为设计尺寸标准。

如果你的设计稿是 375 ,不在以上三种之中,那么你需要把 designWidth 配置为 375,同时在 DEVICE_RATIO 中添加换算规则如下: ```jsx {5} const DEVICE_RATIO = { ‘640’: 2.34 / 2, ‘750’: 1, ‘828’: 1.81 / 2, ‘375’: 2 / 1 }

  1. :::info
  2. Taro v3.4.3 开始支持配置**函数形式**的 `designWidth`,借此开发者可以动态地设置 `designWidth`,详情请查看:[config.designWidth](./config-detail#designwidth)
  3. :::
  4. ## API
  5. 在编译时,Taro 会帮你对样式做尺寸转换操作,但是如果是在 JS 中书写了行内样式,那么编译时就无法做替换了,针对这种情况,Taro 提供了 API `Taro.pxTransform` 来做运行时的尺寸转换。
  6. ```jsx
  7. Taro.pxTransform(10) // 小程序:rpx,H5:rem

配置

默认配置

默认配置会对所有的 px 单位进行转换,有大写字母的 PxPX 则会被忽略。

postcss.pxtransform 的参数默认值如下:

```js title=”config/index.js” config = { mini: { postcss: { pxtransform: { enable: true, config: { onePxTransform: true, unitPrecision: 5, propList: [‘‘], selectorBlackList: [], replace: true, mediaQuery: false, minPixelValue: 0 } } } } h5: { postcss: { pxtransform: { enable: true, config: { onePxTransform: true, unitPrecision: 5, propList: [‘‘], selectorBlackList: [], replace: true, mediaQuery: false, minPixelValue: 0 } } } } }

  1. ### `onePxTransform` (Boolean)
  2. 设置 1px 是否需要被转换
  3. ### `unitPrecision` (Number)
  4. REM 单位允许的小数位。
  5. ### `propList` (Array)
  6. 允许转换的属性。
  7. - Values need to be exact matches.
  8. - Use wildcard `*` to enable all properties. Example: `['*']`
  9. - Use `*` at the start or end of a word. (`['*position*']` will match `background-position-y`)
  10. - Use `!` to not match a property. Example: `['*', '!letter-spacing']`
  11. - Combine the "not" prefix with the other prefixes. Example: `['*', '!font*']`
  12. ### `selectorBlackList`
  13. 黑名单里的选择器将会被忽略。
  14. - If value is string, it checks to see if selector contains the string.
  15. - `['body']` will match `.body-class`
  16. - If value is regexp, it checks to see if the selector matches the regexp.
  17. - `[/^body$/]` will match `body` but not `.body`
  18. ### `replace` (Boolean)
  19. 直接替换而不是追加一条进行覆盖。
  20. ### `mediaQuery` (Boolean)
  21. 允许媒体查询里的 px 单位转换
  22. ### `minPixelValue` (Number)
  23. 设置一个可被转换的最小 px
  24. 配置规则对应到 `config/index.js` ,例如:
  25. ```js {9-14,20-25} title="/config/index.js"
  26. {
  27. h5: {
  28. publicPath: '/',
  29. staticDirectory: 'static',
  30. postcss: {
  31. autoprefixer: {
  32. enable: true
  33. },
  34. pxtransform: {
  35. enable: true,
  36. config: {
  37. selectorBlackList: ['body']
  38. }
  39. }
  40. }
  41. },
  42. mini: {
  43. // ...
  44. postcss: {
  45. pxtransform: {
  46. enable: true,
  47. config: {
  48. selectorBlackList: ['body']
  49. }
  50. }
  51. }
  52. }
  53. }

CSS 编译时忽略(过滤)

忽略单个属性

当前忽略单个属性的最简单的方法,就是 px 单位使用大写字母。

  1. /* `px` is converted to `rem` */
  2. .convert {
  3. font-size: 16px; // converted to 1rem
  4. }
  5. /* `Px` or `PX` is ignored by `postcss-pxtorem` but still accepted by browsers */
  6. .ignore {
  7. border: 1Px solid; // ignored
  8. border-width: 2PX; // ignored
  9. }

忽略样式文件

对于头部包含注释 /*postcss-pxtransform disable*/ 的文件,插件不予处理。

忽略样式举例

样式文件里多行文本省略时我们一般如下面的代码:

```css {3} .textHide { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp:2; text-overflow: ellipsis; overflow: hidden; }

  1. Taro 编译后少了 `-webkit-box-orient: vertical;` 这条样式属性,此时我们需要忽略掉这条样式
  2. #### 忽略样式方法 1 加入 CSS 注释强制声明忽略下一行
  3. ```css {1}
  4. /* autoprefixer: ignore next */
  5. -webkit-box-orient: vertical;

忽略样式方法 2 加入 CSS 注释强制声明注释中间多行

```css {1,3} / autoprefixer: off / -webkit-box-orient: vertical; / autoprefixer: on /

  1. #### 忽略样式方法 3 写成行内样式
  2. ```HTML {2-9}
  3. <View
  4. style={{
  5. display: '-webkit-box',
  6. '-webkit-box-orient': 'vertical',
  7. '-webkit-line-clamp': 2,
  8. 'text-overflow': 'ellipsis',
  9. overflow: 'hidden',
  10. 'line-height': 2
  11. }}
  12. >
  13. 这是要省略的内容这是要省略的内容这是要省略的内容
  14. </View>

相关链接