style-compatibility.mdc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ---
  2. alwaysApply: true
  3. ---
  4. ### 样式兼容性规范 - gap 属性处理
  5. #### 概述
  6. `gap` 属性在 UNI-APP 的 APP 环境中兼容性差,本规范说明如何正确处理间距问题。
  7. #### gap 属性的兼容性问题
  8. **⚠️ 避免使用 gap 属性**
  9. - `gap` 属性在 APP 环境中(特别是 nvue)可能不生效
  10. - 微信小程序对 `gap` 的支持有限制
  11. - 不同平台表现不一致,容易导致布局错乱
  12. #### 推荐做法:使用 margin 代替 gap
  13. **❌ 不推荐的写法**
  14. ```scss
  15. .container {
  16. display: flex;
  17. gap: 20upx; // APP 中可能不生效
  18. }
  19. ```
  20. **✅ 推荐的写法**
  21. ```scss
  22. .container {
  23. display: flex;
  24. flex-direction: column;
  25. }
  26. .item {
  27. margin-top: 20upx;
  28. &:first-child {
  29. margin-top: 0;
  30. }
  31. }
  32. ```
  33. #### 动态间距类的正确写法
  34. **以协议类组件为例**
  35. ```vue
  36. <template>
  37. <view class="content-wrapper">
  38. <view
  39. v-for="(item, index) in items"
  40. :key="index"
  41. :class="{
  42. 'content-item': true,
  43. 'hasGap': item.gap === 2,
  44. 'noGap': item.gap !== 2
  45. }"
  46. >
  47. {{ item.content }}
  48. </view>
  49. </view>
  50. </template>
  51. <style lang="scss" scoped>
  52. .content-item {
  53. padding: 15upx 0;
  54. }
  55. .hasGap {
  56. margin-top: 40upx;
  57. }
  58. .noGap {
  59. margin-top: 0;
  60. }
  61. </style>
  62. ```
  63. #### 条件编译处理 APP 差异
  64. 如果需要区分 APP 和小程序的间距值:
  65. ```scss
  66. .hasGap {
  67. margin-top: 40upx; // 小程序默认值
  68. /* #ifdef APP-PLUS */
  69. margin-top: 50upx; // APP 可能需要更大间距
  70. /* #endif */
  71. }
  72. ```
  73. #### 检查清单
  74. - [ ] 代码中是否使用了 `gap` 属性?→ 改用 `margin`
  75. - [ ] 是否为 Flex 容器的第一个子元素设置了 `margin: 0`?
  76. - [ ] 是否需要针对 APP 调整间距值?→ 使用条件编译