uni-mall-head.vue 4.4 KB

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