myTabbar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false"
  3. :scroll-into-view="scrollInto" :style="{height: height+'rpx'}">
  4. <view style="flex-direction: column;">
  5. <view style="flex-direction: row;">
  6. <view class="uni-tab-item" v-for="(tab,index) in tabList" :key="index" :id="'tab'+index"
  7. :ref="'tabitem'+index" :data-id="index" :data-current="index" @click="handleTab">
  8. <text class="uni-tab-item-title" :class="current==index ? 'uni-tab-item-title-active' : ''"
  9. :style="{height: (height - 6) + 'rpx',lineHeight: (height - 6) + 'rpx'}">{{tab.name}}</text>
  10. </view>
  11. </view>
  12. <view class="scroll-view-indicator">
  13. <view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''"
  14. :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}">
  15. <view class="tabbar-tabline-border"></view>
  16. </view>
  17. </view>
  18. </view>
  19. </scroll-view>
  20. </template>
  21. <script>
  22. export default {
  23. name:"myTabbar",
  24. props: {
  25. current: {
  26. type: [Number,String],
  27. default: 0
  28. },
  29. tabList: {
  30. type: Array,
  31. default: []
  32. },
  33. // 是否可以滚动
  34. scrollable: {
  35. type: Boolean,
  36. default: false
  37. },
  38. // tab-bar高度
  39. height: {
  40. type: Number,
  41. default: 100
  42. },
  43. },
  44. data() {
  45. return {
  46. scrollInto: "",
  47. isTap: false,
  48. tabListSize: {},
  49. indicatorLineLeft: 0,
  50. indicatorLineWidth: 0
  51. };
  52. },
  53. watch: {
  54. current(val,old) {
  55. if(val == old) return
  56. this.scrollInto = 'tab'+this.current;
  57. },
  58. tabList: {
  59. handler() {
  60. this.tabListSize = {};
  61. this.$nextTick(()=>{
  62. this.scrollInto = 'tab'+this.current;
  63. this.selectorQuery();
  64. })
  65. },
  66. immediate: true
  67. }
  68. },
  69. methods: {
  70. swiperChange(index,isTap) {
  71. this.isTap = isTap
  72. this.updateIndicator(this.tabListSize['tab'+index].left, this.tabListSize['tab'+index].width);
  73. this.$emit("change",{
  74. index: index,
  75. isTap: this.isTap
  76. })
  77. },
  78. handleTab(e) {
  79. let index = e.target.dataset.current || e.currentTarget.dataset.current;
  80. this.isTap = true;
  81. var currentSize = this.tabListSize['tab'+index];
  82. this.updateIndicator(currentSize.left, currentSize.width);
  83. this._touchTabIndex = index;
  84. this.$emit("change",{
  85. index: index,
  86. isTap: this.isTap
  87. })
  88. },
  89. updateIndicator(left, width) {
  90. this.indicatorLineLeft = left;
  91. this.indicatorLineWidth = width;
  92. },
  93. selectorQuery() {
  94. if(this.tabList&&this.tabList.length > 0) {
  95. uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
  96. rects.forEach((rect) => {
  97. this.tabListSize[rect.id] = rect;
  98. })
  99. this.updateIndicator(this.tabListSize['tab'+this.current].left, this.tabListSize['tab'+this.current].width);
  100. }).exec();
  101. }
  102. },
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. @mixin u-flex($flexD, $alignI, $justifyC) {
  108. display: flex;
  109. flex-direction: $flexD;
  110. align-items: $alignI;
  111. justify-content: $justifyC;
  112. }
  113. .tab-bar {
  114. width: 100vw;
  115. height: 100rpx;
  116. background-color: #fff;
  117. flex-direction: row;
  118. white-space: nowrap;
  119. font-family: PingFang SC, PingFang SC;
  120. font-weight: 400;
  121. font-size: 28rpx;
  122. color: #757575;
  123. }
  124. .tab-bar ::-webkit-scrollbar {
  125. display: none;
  126. width: 0 !important;
  127. height: 0 !important;
  128. -webkit-appearance: none;
  129. background: transparent;
  130. }
  131. .uni-tab-item {
  132. display: inline-block;
  133. flex-wrap: nowrap;
  134. min-width: 125rpx;
  135. box-sizing: border-box;
  136. text-align: center;
  137. padding-left: 20rpx;
  138. padding-right: 20rpx;
  139. }
  140. .uni-tab-item-title {
  141. height: 94rpx;
  142. line-height: 94rpx;
  143. flex-wrap: nowrap;
  144. white-space: nowrap;
  145. }
  146. .uni-tab-item-title-active {
  147. font-weight: 500;
  148. color: #333333;
  149. }
  150. .scroll-view-indicator {
  151. position: relative;
  152. height: 6rpx;
  153. background-color: transparent;
  154. }
  155. .scroll-view-underline {
  156. position: absolute;
  157. top: 0;
  158. bottom: 0;
  159. width: 0;
  160. display: flex;
  161. align-items: center;
  162. justify-content: center;
  163. }
  164. .tabbar-tabline-border {
  165. width: 48rpx;
  166. height: 6rpx;
  167. background: #FF7700;
  168. border-radius: 3rpx 3rpx 3rpx 3rpx;
  169. }
  170. .scroll-view-animation {
  171. transition-duration: 0.2s;
  172. transition-property: left;
  173. }
  174. </style>