u-tabbar.vue 4.0 KB

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