uni-mall-head.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="me-tabs">
  3. <scroll-view :id="viewId" :scroll-left="scrollLeft" scroll-x scroll-with-animation
  4. :scroll-animation-duration="300">
  5. <view class="tabs-item tabs-flex tabs-scroll">
  6. <view class="tab-item" v-for="(tab, i) in tabs" :key="i" @click="tabClick(i)" :ref="'refTabItem' + i">
  7. <text>{{tab.name}}</text>
  8. <uni-icons class="inherit-icons" type="right" color="#8c8c8c" v-if="i < tabs.length - 1">
  9. </uni-icons>
  10. <view class="line" v-else></view>
  11. </view>
  12. </view>
  13. </scroll-view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. tabs: [],
  21. viewId: 'id_' + Math.random().toString(36).substr(2, 16),
  22. scrollLeft: 0,
  23. warpWidth: 0
  24. };
  25. },
  26. computed: {
  27. isScroll() {
  28. return this.tabWidth && this.tabs.length; // 指定了tabWidth的宽度,则支持水平滑动
  29. },
  30. tabHeightPx() {
  31. return uni.upx2px(this.height);
  32. },
  33. tabHeightVal() {
  34. return 48 + 'px';
  35. },
  36. tabWidthPx() {
  37. return uni.upx2px(this.tabWidth);
  38. },
  39. tabWidthVal() {
  40. return this.isScroll ? this.tabWidthPx + 'px' : '';
  41. },
  42. lineLeft() {
  43. if (this.isScroll) {
  44. return this.tabWidthPx * this.value + this.tabWidthPx / 2 + 'px'; // 需转为px (用rpx的话iOS真机显示有误差)
  45. } else {
  46. return (100 / this.tabs.length) * (this.value + 1) - 100 / (this.tabs.length * 2) + '%';
  47. }
  48. }
  49. },
  50. watch: {
  51. value() {
  52. this.scrollCenter(); // 水平滚动到中间
  53. }
  54. },
  55. methods: {
  56. tabClick(i) {
  57. this.tabs.splice(i + 1, this.tabs.length);
  58. this.$emit('change', this.tabs[i]);
  59. },
  60. async addTab(item) {
  61. this.tabs.push(item);
  62. setTimeout(() => {
  63. let query = uni.createSelectorQuery();
  64. query = query.in(this); // 支付宝小程序不支持in(this),而字节跳动小程序必须写in(this), 否则都取不到值
  65. query
  66. .select('.tab-item')
  67. .boundingClientRect(data => {
  68. this.warpWidth += data.width;
  69. this.scrollLeft = this.warpWidth;
  70. })
  71. .exec();
  72. }, 100);
  73. },
  74. async scrollCenter() {
  75. if (!this.isScroll) return;
  76. if (!this.warpWidth) {
  77. // tabs容器的宽度
  78. let rect = await this.initWarpRect();
  79. this.warpWidth = rect ? rect.width : uni.getSystemInfoSync().windowWidth; // 某些情况下取不到宽度,暂时取屏幕宽度
  80. }
  81. let tabLeft = this.tabWidthPx * this.value + this.tabWidthPx / 2; // 当前tab中心点到左边的距离
  82. let diff = tabLeft - this.warpWidth / 2; // 如果超过tabs容器的一半,则滚动差值
  83. this.scrollLeft = diff;
  84. // #ifdef MP-TOUTIAO
  85. this.scrollTimer && clearTimeout(this.scrollTimer);
  86. this.scrollTimer = setTimeout(() => {
  87. // 字节跳动小程序,需延时再次设置scrollLeft,否则tab切换跨度较大时不生效
  88. this.scrollLeft = Math.ceil(diff);
  89. }, 400);
  90. // #endif
  91. },
  92. initWarpRect() {
  93. return new Promise(resolve => {
  94. setTimeout(() => {
  95. // 延时确保dom已渲染, 不使用$nextclick
  96. let query = uni.createSelectorQuery();
  97. // #ifndef MP-ALIPAY
  98. query = query.in(this); // 支付宝小程序不支持in(this),而字节跳动小程序必须写in(this), 否则都取不到值
  99. // #endif
  100. query
  101. .select('#' + this.viewId)
  102. .boundingClientRect(data => {
  103. resolve(data);
  104. })
  105. .exec();
  106. }, 20);
  107. });
  108. }
  109. },
  110. mounted() {
  111. this.scrollCenter(); // 滚动到当前下标
  112. }
  113. };
  114. </script>
  115. <style lang="scss">
  116. .me-tabs {
  117. position: relative;
  118. background-color: #fff;
  119. box-sizing: border-box;
  120. overflow-y: hidden;
  121. height: 96rpx;
  122. &.tabs-fixed {
  123. z-index: 990;
  124. position: fixed;
  125. top: var(--window-top);
  126. left: 0;
  127. width: 100%;
  128. }
  129. .tabs-item {
  130. position: relative;
  131. white-space: nowrap;
  132. box-sizing: border-box;
  133. }
  134. // 平分的方式显示item
  135. .tabs-flex {
  136. display: flex;
  137. .tab-item {
  138. margin-right: 20rpx;
  139. position: relative;
  140. color: $uni-primary;
  141. position: relative;
  142. text-align: center;
  143. box-sizing: border-box;
  144. height: 96rpx;
  145. display: flex;
  146. align-items: center;
  147. &:last-child {
  148. margin-right: 0px;
  149. color: $uni-secondary-color;
  150. }
  151. .inherit-icons {
  152. margin-left: 20rpx;
  153. }
  154. .line {
  155. width: 40rpx;
  156. }
  157. }
  158. }
  159. }
  160. </style>