wk-tabbar.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view class="wk-tabbar">
  3. <button
  4. v-for="(item, index) in list"
  5. :key="index"
  6. :class="selected === index ? (activeClass || 'is-selected') : ''"
  7. class="wk-tab-item"
  8. @click="_handleSelect(index)">
  9. <template v-if="selected !== index">
  10. <template v-if="item.icon">
  11. <text
  12. v-if="item.icon.startsWith('ft ')"
  13. :class="item.icon"
  14. class="font-icon" />
  15. <text
  16. v-else
  17. :class="item.icon"
  18. class="wk font-icon" />
  19. </template>
  20. <image v-else :src="$static(item.src)" class="nav-icon" />
  21. </template>
  22. <template v-else>
  23. <template v-if="item.active_icon">
  24. <text
  25. v-if="item.icon.startsWith('ft ')"
  26. :class="item.active_icon"
  27. class="font-icon" />
  28. <text
  29. v-else
  30. :class="item.active_icon"
  31. class="wk font-icon" />
  32. </template>
  33. <image v-else :src="$static(item.active_src || item.src)" class="nav-icon" />
  34. </template>
  35. <view v-if="item.badge" class="badge">
  36. <!-- {{ item.badge }} -->
  37. </view>
  38. <view class="nav-desc">
  39. {{ item.name }}
  40. </view>
  41. </button>
  42. </view>
  43. </template>
  44. <script>
  45. /**
  46. * 底部导航栏点击切换
  47. * props:
  48. * list {Array} 导航数组 [{Object}]
  49. * name: {String} 导航描述信息
  50. * src: {String} 导航图标,其中 icon 不为空时该项不生效
  51. * active_src: {String} 选中后的图标,其中 active_icon 不为空时该项不生效
  52. * icon {String} 导航字体图标
  53. * active_icon {String} 选中后的导航字体图标
  54. * path: {String} 点击该项时要跳转的路由地址 path 和 fn 互斥
  55. * query: {Object} 路由跳转时查询参数
  56. * fn: {Function} 点击该项时要执行的函数
  57. * params: {Object} 点击该项要执行的函数的参数
  58. * active-class {String} 选中后的class名
  59. * default-value {Number} 默认选中第几个
  60. */
  61. export default {
  62. name: 'WkTabbar',
  63. props: {
  64. value: {
  65. type: Number,
  66. default: 0
  67. },
  68. list: {
  69. type: Array,
  70. required: true
  71. },
  72. activeClass: {
  73. type: String,
  74. default: null
  75. },
  76. },
  77. data() {
  78. return {
  79. selected: null, // 当前选中第几个
  80. maskStatus: false
  81. }
  82. },
  83. watch: {
  84. value: {
  85. handler(val) {
  86. console.log('tab selected', val)
  87. this.selected = val
  88. },
  89. deep: true,
  90. immediate: true
  91. }
  92. },
  93. mounted() {
  94. uni.$on('mask-evt', status => {
  95. this.maskStatus = status
  96. })
  97. console.log('tab: ', this)
  98. },
  99. beforeDestroy() {
  100. uni.$off('mask-evt')
  101. },
  102. methods: {
  103. /**
  104. * 导航切换选择
  105. * @private
  106. */
  107. _handleSelect(index) {
  108. console.log('tabbar click')
  109. if (this.maskStatus) {
  110. this.$nextTick(function() {
  111. uni.$emit('mask-evt', false)
  112. })
  113. return
  114. }
  115. if (index === this.selected) return
  116. this.selected = index
  117. this.$emit('input', index)
  118. const item = this.list[index]
  119. if (item.fn && typeof item.fn === 'function') {
  120. item.fn(item.params || null)
  121. } else if (item.path) {
  122. this.$Router.reLaunch({
  123. url: item.path,
  124. query: item.query ? item.query : null
  125. })
  126. }
  127. },
  128. addMask() {
  129. },
  130. removeMask() {}
  131. }
  132. }
  133. </script>
  134. <style scoped lang="scss">
  135. .wk-tabbar {
  136. position: relative;
  137. z-index: 10;
  138. width: 100%;
  139. height: $tabbar-height;
  140. background-color: white;
  141. border-top: 1rpx solid #F1F1F1;
  142. display: flex;
  143. align-items: center;
  144. justify-content: space-between;
  145. .wk-tab-item {
  146. flex: 1;
  147. position: relative;
  148. height: 100%;
  149. padding: 0;
  150. flex-direction: column;
  151. border-width: 0;
  152. @include center;
  153. .font-icon {
  154. font-size: 38rpx;
  155. color: #4E565E;
  156. display: block;
  157. }
  158. .nav-icon {
  159. width: 38rpx;
  160. height: 38rpx;
  161. }
  162. .nav-desc {
  163. font-size: 24rpx;
  164. color: #4E565E;
  165. }
  166. .badge {
  167. position: absolute;
  168. top: 10rpx;
  169. left: calc(50% + 15rpx);
  170. width: 18rpx;
  171. height: 18rpx;
  172. border-radius: 50%;
  173. background-color: $error;
  174. // min-width: 32rpx;
  175. // height: 32rpx;
  176. // font-size: 22rpx;
  177. // color: white;
  178. // padding: 0 10rpx;
  179. // background-color: $error;
  180. // border-radius: 32rpx;
  181. // @include center;
  182. }
  183. &.is-selected {
  184. background-color: white;
  185. .font-icon, .nav-desc {
  186. color: $theme-color;
  187. }
  188. }
  189. }
  190. }
  191. </style>