Taro 設(shè)計稿及尺寸單位

2021-09-23 20:56 更新

在 Taro 中尺寸單位建議使用 px、 百分比 %,Taro 默認(rèn)會對所有單位進(jìn)行轉(zhuǎn)換。在 Taro 中書寫尺寸按照 1:1 的關(guān)系來進(jìn)行書寫,即從設(shè)計稿上量的長度 100px,那么尺寸書寫就是 100px,當(dāng)轉(zhuǎn)成微信小程序的時候,尺寸將默認(rèn)轉(zhuǎn)換為 100rpx,當(dāng)轉(zhuǎn)成 H5 時將默認(rèn)轉(zhuǎn)換為以 rem 為單位的值。

如果你希望部分 px 單位不被轉(zhuǎn)換成 rpx 或者 rem ,最簡單的做法就是在 px 單位中增加一個大寫字母,例如 Px 或者 PX 這樣,則會被轉(zhuǎn)換插件忽略。

結(jié)合過往的開發(fā)經(jīng)驗(yàn),Taro 默認(rèn)以 750px 作為換算尺寸標(biāo)準(zhǔn),如果設(shè)計稿不是以 750px 為標(biāo)準(zhǔn),則需要在項(xiàng)目配置 config/index.js 中進(jìn)行設(shè)置,例如設(shè)計稿尺寸是 640px,則需要修改項(xiàng)目配置 config/index.js 中的 designWidth 配置為 640

  1. const config = {
  2. projectName: 'myProject',
  3. date: '2018-4-18',
  4. designWidth: 640,
  5. ....
  6. }

目前 Taro 支持 750、 640 、 828 三種尺寸設(shè)計稿,他們的換算規(guī)則如下:

  1. const DEVICE_RATIO = {
  2. '640': 2.34 / 2,
  3. '750': 1,
  4. '828': 1.81 / 2
  5. }

建議使用 Taro 時,設(shè)計稿以 iPhone 6 750px 作為設(shè)計尺寸標(biāo)準(zhǔn)。

如果你的設(shè)計稿是 375 ,不在以上三種之中,那么你需要把 designWidth 配置為 375,同時在 DEVICE_RATIO 中添加換算規(guī)則如下:

  1. const DEVICE_RATIO = {
  2. '640': 2.34 / 2,
  3. '750': 1,
  4. '828': 1.81 / 2,
  5. '375': 2 / 1
  6. }

API

在編譯時,Taro 會幫你對樣式做尺寸轉(zhuǎn)換操作,但是如果是在 JS 中書寫了行內(nèi)樣式,那么編譯時就無法做替換了,針對這種情況,Taro 提供了 API Taro.pxTransform 來做運(yùn)行時的尺寸轉(zhuǎn)換。

  1. Taro.pxTransform(10) // 小程序:rpx,H5:rem

配置

默認(rèn)配置會對所有的 px 單位進(jìn)行轉(zhuǎn)換,有大寫字母的 PxPX 則會被忽略。

參數(shù)默認(rèn)值如下:

  1. {
  2. onePxTransform: true,
  3. unitPrecision: 5,
  4. propList: ['*'],
  5. selectorBlackList: [],
  6. replace: true,
  7. mediaQuery: false,
  8. minPixelValue: 0
  9. }

Type: Object | Null

onePxTransform (Boolean)

設(shè)置 1px 是否需要被轉(zhuǎn)換

unitPrecision (Number)

REM 單位允許的小數(shù)位。

propList (Array)

允許轉(zhuǎn)換的屬性。

  • Values need to be exact matches.
  • Use wildcard * to enable all properties. Example: ['*']
  • Use * at the start or end of a word. (['*position*'] will match background-position-y)
  • Use ! to not match a property. Example: ['*', '!letter-spacing']
  • Combine the “not” prefix with the other prefixes. Example: ['*', '!font*']

selectorBlackList

黑名單里的選擇器將會被忽略。

  • If value is string, it checks to see if selector contains the string.
    • ['body'] will match .body-class
  • If value is regexp, it checks to see if the selector matches the regexp.
    • [/^body$/] will match body but not .body

replace (Boolean)

直接替換而不是追加一條進(jìn)行覆蓋。

mediaQuery (Boolean)

允許媒體查詢里的 px 單位轉(zhuǎn)換

minPixelValue (Number)

設(shè)置一個可被轉(zhuǎn)換的最小 px 值

配置規(guī)則對應(yīng)到 config/index.js ,例如:

  1. {
  2. h5: {
  3. publicPath: '/',
  4. staticDirectory: 'static',
  5. module: {
  6. postcss: {
  7. autoprefixer: {
  8. enable: true
  9. },
  10. pxtransform: {
  11. enable: true,
  12. config: {
  13. selectorBlackList: ['body']
  14. }
  15. }
  16. }
  17. }
  18. },
  19. weapp: {
  20. // ...
  21. module: {
  22. postcss: {
  23. pxtransform: {
  24. enable: true,
  25. config: {
  26. selectorBlackList: ['body']
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }

忽略

屬性

當(dāng)前忽略單個屬性的最簡單的方法,就是 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*/ 的文件,插件不予處理。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號