u-tabbar.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="u-tabbar">
  3. <view
  4. class="u-tabbar__content"
  5. ref="u-tabbar__content"
  6. @touchmove.stop.prevent="noop"
  7. :class="[border && 'u-border-top', fixed && 'u-tabbar--fixed']"
  8. :style="[tabbarStyle]"
  9. >
  10. <view class="u-tabbar__content__item-wrapper">
  11. <slot />
  12. </view>
  13. <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom>
  14. </view>
  15. <view
  16. class="u-tabbar__placeholder"
  17. v-if="placeholder"
  18. :style="{
  19. height: placeholderHeight + 'px',
  20. }"
  21. ></view>
  22. </view>
  23. </template>
  24. <script>
  25. import { props } from './props';
  26. import { mpMixin } from '../../libs/mixin/mpMixin';
  27. import { mixin } from '../../libs/mixin/mixin';
  28. import { addStyle, deepMerge, sleep } from '../../libs/function/index';
  29. // #ifdef APP-NVUE
  30. const dom = uni.requireNativePlugin('dom')
  31. // #endif
  32. /**
  33. * Tabbar 底部导航栏
  34. * @description 此组件提供了自定义tabbar的能力。
  35. * @tutorial https://ijry.github.io/uview-plus/components/tabbar.html
  36. * @property {String | Number} value 当前匹配项的name
  37. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离(默认 true )
  38. * @property {Boolean} border 是否显示上方边框(默认 true )
  39. * @property {String | Number} zIndex 元素层级z-index(默认 1 )
  40. * @property {String} activeColor 选中标签的颜色(默认 '#1989fa' )
  41. * @property {String} inactiveColor 未选中标签的颜色(默认 '#7d7e80' )
  42. * @property {Boolean} fixed 是否固定在底部(默认 true )
  43. * @property {Boolean} placeholder fixed定位固定在底部时,是否生成一个等高元素防止塌陷(默认 true )
  44. * @property {String} backgroundColor 背景色(默认 '#ffffff' )
  45. * @property {Object} customStyle 定义需要用到的外部样式
  46. *
  47. * @example <u-tabbar :value="value2" :placeholder="false" @change="name => value2 = name" :fixed="false" :safeAreaInsetBottom="false"><u-tabbar-item text="首页" icon="home" dot ></u-tabbar-item></u-tabbar>
  48. */
  49. export default {
  50. name: 'u-tabbar',
  51. mixins: [mpMixin, mixin, props],
  52. data() {
  53. return {
  54. placeholderHeight: 0
  55. }
  56. },
  57. computed: {
  58. tabbarStyle() {
  59. const style = {
  60. zIndex: this.zIndex
  61. }
  62. if (this.borderColor) {
  63. style.borderColor = this.borderColor
  64. }
  65. if (this.backgroundColor) {
  66. style.backgroundColor = this.backgroundColor
  67. }
  68. // 合并来自父组件的customStyle样式
  69. return deepMerge(style, addStyle(this.customStyle))
  70. },
  71. // 监听多个参数的变化,通过在computed执行对应的操作
  72. updateChild() {
  73. return [this.value, this.activeColor, this.inactiveColor]
  74. },
  75. updatePlaceholder() {
  76. return [this.fixed, this.placeholder]
  77. }
  78. },
  79. watch: {
  80. updateChild() {
  81. // 如果updateChildren中的元素发生了变化,则执行子元素初始化操作
  82. this.updateChildren()
  83. },
  84. updatePlaceholder() {
  85. // 如果fixed,placeholder等参数发生变化,重新计算占位元素的高度
  86. this.setPlaceholderHeight()
  87. }
  88. },
  89. created() {
  90. this.children = []
  91. },
  92. mounted() {
  93. this.setPlaceholderHeight()
  94. },
  95. methods: {
  96. updateChildren() {
  97. // 如果存在子元素,则执行子元素的updateFromParent进行更新数据
  98. this.children.length && this.children.map(child => child.updateFromParent())
  99. },
  100. // 设置用于防止塌陷元素的高度
  101. async setPlaceholderHeight() {
  102. if (!this.fixed || !this.placeholder) return
  103. // 延时一定时间
  104. await sleep(20)
  105. // #ifndef APP-NVUE
  106. this.$uGetRect('.u-tabbar__content').then(({height = 50}) => {
  107. // 修复IOS safearea bottom 未填充高度
  108. this.placeholderHeight = height
  109. })
  110. // #endif
  111. // #ifdef APP-NVUE
  112. dom.getComponentRect(this.$refs['u-tabbar__content'], (res) => {
  113. const {
  114. size
  115. } = res
  116. this.placeholderHeight = size.height
  117. })
  118. // #endif
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. .u-tabbar {
  125. @include flex(column);
  126. flex: 1;
  127. justify-content: center;
  128. &__content {
  129. @include flex(column);
  130. background-color: #fff;
  131. &__item-wrapper {
  132. height: 50px;
  133. @include flex(row);
  134. justify-content: space-around;
  135. }
  136. }
  137. &--fixed {
  138. position: fixed;
  139. bottom: 0;
  140. left: 0;
  141. right: 0;
  142. }
  143. }
  144. </style>