su-tabbar.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!-- 底部导航栏 -->
  2. <template>
  3. <view class="u-tabbar">
  4. <view class="u-tabbar__content" ref="u-tabbar__content" @touchmove.stop.prevent=""
  5. :class="[border && 'u-border-top', fixed && 'u-tabbar--fixed', { 'mid-tabbar': midTabBar }]"
  6. :style="[tabbarStyle]">
  7. <view class="u-tabbar__content__item-wrapper">
  8. <slot></slot>
  9. </view>
  10. <view v-if="safeAreaInsetBottom" :style="[{ height: safeBottomHeight + 'px' }]"></view>
  11. </view>
  12. <view class="u-tabbar__placeholder" v-if="placeholder" :style="{
  13. height: placeholderHeight + 'px',
  14. }"></view>
  15. </view>
  16. </template>
  17. <script>
  18. // #ifdef APP-NVUE
  19. const dom = uni.requireNativePlugin('dom');
  20. // #endif
  21. /**
  22. * Tabbar 底部导航栏
  23. * @description 此组件提供了自定义tabbar的能力。
  24. * @property {String | Number} value 当前匹配项的name
  25. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离(默认 true )
  26. * @property {Boolean} border 是否显示上方边框(默认 true )
  27. * @property {String | Number} zIndex 元素层级z-index(默认 1 )
  28. * @property {String} activeColor 选中标签的颜色(默认 '#1989fa' )
  29. * @property {String} inactiveColor 未选中标签的颜色(默认 '#7d7e80' )
  30. * @property {Boolean} fixed 是否固定在底部(默认 true )
  31. * @property {Boolean} placeholder fixed定位固定在底部时,是否生成一个等高元素防止塌陷(默认 true )
  32. * @property {Object} customStyle 定义需要用到的外部样式
  33. *
  34. */
  35. import {
  36. deepMerge,
  37. addStyle,
  38. sleep
  39. } from '@/sheep/helper';
  40. import sheep from '@/sheep';
  41. export default {
  42. name: 'su-tabbar',
  43. props: {
  44. customStyle: {
  45. type: [Object, String],
  46. default: () => ({}),
  47. },
  48. customClass: {
  49. type: String,
  50. default: '',
  51. },
  52. // 跳转的页面路径
  53. url: {
  54. type: String,
  55. default: '',
  56. },
  57. // 页面跳转的类型
  58. linkType: {
  59. type: String,
  60. default: 'navigateTo',
  61. },
  62. // 当前匹配项的name
  63. value: {
  64. type: [String, Number, null],
  65. default: '',
  66. },
  67. // 是否为iPhoneX留出底部安全距离
  68. safeAreaInsetBottom: {
  69. type: Boolean,
  70. default: true,
  71. },
  72. // 是否显示上方边框
  73. border: {
  74. type: Boolean,
  75. default: true,
  76. },
  77. // 元素层级z-index
  78. zIndex: {
  79. type: [String, Number],
  80. default: 10,
  81. },
  82. // 选中标签的颜色
  83. activeColor: {
  84. type: String,
  85. default: '#1989fa',
  86. },
  87. // 未选中标签的颜色
  88. inactiveColor: {
  89. type: String,
  90. default: '#7d7e80',
  91. },
  92. // 是否固定在底部
  93. fixed: {
  94. type: Boolean,
  95. default: true,
  96. },
  97. // fixed定位固定在底部时,是否生成一个等高元素防止塌陷
  98. placeholder: {
  99. type: Boolean,
  100. default: true,
  101. },
  102. midTabBar: {
  103. type: Boolean,
  104. default: false,
  105. },
  106. },
  107. data() {
  108. return {
  109. placeholderHeight: 0,
  110. safeBottomHeight: sheep.$platform.device.safeAreaInsets.bottom,
  111. };
  112. },
  113. computed: {
  114. tabbarStyle() {
  115. const style = {
  116. zIndex: this.zIndex,
  117. };
  118. // 合并来自父组件的customStyle样式
  119. return deepMerge(style, addStyle(this.customStyle));
  120. },
  121. // 监听多个参数的变化,通过在computed执行对应的操作
  122. updateChild() {
  123. return [this.value, this.activeColor, this.inactiveColor];
  124. },
  125. color() {
  126. return this.inactiveColor;
  127. },
  128. updatePlaceholder() {
  129. return [this.fixed, this.placeholder];
  130. },
  131. },
  132. watch: {
  133. updateChild() {
  134. // 如果updateChildren中的元素发生了变化,则执行子元素初始化操作
  135. this.updateChildren();
  136. },
  137. updatePlaceholder() {
  138. // 如果fixed,placeholder等参数发生变化,重新计算占位元素的高度
  139. this.setPlaceholderHeight();
  140. },
  141. },
  142. created() {
  143. this.children = [];
  144. },
  145. mounted() {
  146. this.setPlaceholderHeight();
  147. },
  148. methods: {
  149. updateChildren() {
  150. // 如果存在子元素,则执行子元素的updateFromParent进行更新数据
  151. this.children.length && this.children.map((child) => child.updateFromParent());
  152. },
  153. getRect(selector, all) {
  154. return new Promise((resolve) => {
  155. uni.createSelectorQuery()
  156. .in(this)[all ? 'selectAll' : 'select'](selector)
  157. .boundingClientRect((rect) => {
  158. if (all && Array.isArray(rect) && rect.length) {
  159. resolve(rect);
  160. }
  161. if (!all && rect) {
  162. resolve(rect);
  163. }
  164. })
  165. .exec();
  166. });
  167. },
  168. // 设置用于防止塌陷元素的高度
  169. async setPlaceholderHeight() {
  170. if (!this.fixed || !this.placeholder) return;
  171. // 延时一定时间
  172. await sleep(20);
  173. // #ifndef APP-NVUE
  174. this.getRect('.u-tabbar__content').then(({
  175. height = 50
  176. }) => {
  177. // 修复IOS safearea bottom 未填充高度
  178. this.placeholderHeight = height;
  179. });
  180. // #endif
  181. // #ifdef APP-NVUE
  182. dom.getComponentRect(this.$refs['u-tabbar__content'], (res) => {
  183. const {
  184. size
  185. } = res;
  186. this.placeholderHeight = size.height;
  187. });
  188. // #endif
  189. },
  190. },
  191. };
  192. </script>
  193. <style lang="scss" scoped>
  194. .u-tabbar {
  195. display: flex;
  196. flex: 1;
  197. justify-content: center;
  198. &__content {
  199. display: flex;
  200. flex-direction: column;
  201. background-color: #fff;
  202. box-shadow: 0px -2px 4px 0px rgba(51, 51, 51, 0.06);
  203. &__item-wrapper {
  204. height: 50px;
  205. display: flex;
  206. justify-content: space-around;
  207. align-items: center;
  208. }
  209. }
  210. .mid-tabbar {
  211. border-radius: 30rpx 30rpx 0 0;
  212. }
  213. &--fixed {
  214. position: fixed;
  215. bottom: -1px;
  216. left: 0;
  217. right: 0;
  218. }
  219. }
  220. </style>