liuyuno-tabs.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view class="tab-box" id="tab-box">
  3. <view class="horizontal">
  4. <scroll-view :scroll-x="true" style="white-space: nowrap; " scroll-with-animation
  5. :scroll-left="slider.scrollLeft">
  6. <view v-for="(item, index) in tabList" :key="index" style="display: inline-block;">
  7. <block>
  8. <view class="flex-grow-1 flex-center">
  9. <view class="item" :class="{ active: activeIndex === index }" :id="'tab_'+index"
  10. @click="tabClick(index)">{{ item.name || item }}
  11. </view>
  12. </view>
  13. </block>
  14. </view>
  15. <view class="underline"
  16. :style="'transform:translateX(' + slider.left + 'px);width:' + slider.width + 'px'"></view>
  17. </scroll-view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'liuyuno-tabs',
  24. props: {
  25. tabData: {
  26. type: Array,
  27. default: () => []
  28. },
  29. defaultIndex: {
  30. type: Number,
  31. default: 0
  32. },
  33. underLinePadding: {
  34. type: Number,
  35. default: 10
  36. },
  37. },
  38. data() {
  39. return {
  40. tabList: [],
  41. tabListSlider: [],
  42. box: {
  43. left: 0,
  44. right: 0,
  45. top: 0,
  46. width: 0,
  47. height: 0,
  48. bottom: 0,
  49. },
  50. slider: {
  51. left: 0,
  52. width: 0,
  53. scrollLeft: 0
  54. },
  55. activeIndex: 0
  56. };
  57. },
  58. watch: {
  59. tabData(value) {
  60. this.tabList = value;
  61. setTimeout(() => {
  62. this.updateTabWidth();
  63. }, 100);
  64. },
  65. },
  66. mounted() {
  67. this.tabList = this.tabData;
  68. this.activeIndex = this.defaultIndex;
  69. this.$nextTick(() => {
  70. // #ifdef MP-ALIPAY
  71. var query = my.createSelectorQuery()
  72. query.select('.tab-box').boundingClientRect().exec((ret) => {
  73. // console.log(ret[0], "tab-box")
  74. let res=ret[0];
  75. this.box =res;
  76. this.updateTabWidth();
  77. })
  78. // #endif
  79. // #ifdef MP-WEIXIN
  80. var query = uni.createSelectorQuery().in(this);
  81. query.select('.tab-box').boundingClientRect((res) => {
  82. // console.log(res, "tab-box")
  83. this.box = res;
  84. this.updateTabWidth();
  85. }).exec();
  86. // #endif
  87. })
  88. },
  89. methods: {
  90. tabClick(index) {
  91. this.activeIndex = index;
  92. this.tabToIndex(index);
  93. this.$emit('tabClick', index);
  94. },
  95. tabToIndex(index) {
  96. let _slider = this.tabListSlider[index];
  97. this.slider = {
  98. left: _slider.left + (_slider.width / 2) - (_slider.width / 4),
  99. width: (_slider.width) / 2,
  100. scrollLeft: _slider.scrollLeft,
  101. // left: _slider.left + this.underLinePadding,
  102. // width: _slider.width - this.underLinePadding * 2,
  103. // scrollLeft: _slider.scrollLeft,
  104. }
  105. },
  106. updateTabWidth(index = 0) {
  107. let data = this.tabList;
  108. if (data.length == 0) return false;
  109. // console.log(data, "data")
  110. // #ifdef MP-ALIPAY
  111. var query = my.createSelectorQuery()
  112. query.select('#tab_' + index).boundingClientRect().exec((ret) => {
  113. // console.log(ret[0], "tab-box")
  114. let res=ret[0];
  115. let _prev_slider = this.tabListSlider[index - 1];
  116. this.tabListSlider[index] = {
  117. left: res.left - this.box.left,
  118. width: res.width,
  119. scrollLeft: res.left - this.box.left - (_prev_slider ? _prev_slider.width : 0),
  120. }
  121. if (this.activeIndex == index) {
  122. this.tabToIndex(this.activeIndex);
  123. }
  124. index++;
  125. if (data.length > index) {
  126. this.updateTabWidth(index);
  127. }
  128. })
  129. // #endif
  130. // #ifdef MP-WEIXIN
  131. var query = uni.createSelectorQuery().in(this);
  132. query.select('#tab_' + index).boundingClientRect((res) => {
  133. let _prev_slider = this.tabListSlider[index - 1];
  134. this.tabListSlider[index] = {
  135. left: res.left - this.box.left,
  136. width: res.width,
  137. scrollLeft: res.left - this.box.left - (_prev_slider ? _prev_slider.width : 0),
  138. }
  139. if (this.activeIndex == index) {
  140. this.tabToIndex(this.activeIndex);
  141. }
  142. index++;
  143. if (data.length > index) {
  144. this.updateTabWidth(index);
  145. }
  146. }).exec();
  147. // #endif
  148. }
  149. }
  150. }
  151. </script>
  152. <style lang="scss">
  153. .tab-box {
  154. width: 100%;
  155. color: rgba(0, 0, 0, 0.8);
  156. display: flex;
  157. height: 100rpx;
  158. background: #fff;
  159. font-size: 28upx;
  160. box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
  161. position: relative;
  162. z-index: 10;
  163. overflow: hidden;
  164. }
  165. .horizontal .active {
  166. color: #000;
  167. }
  168. .horizontal {
  169. width: 100%;
  170. }
  171. .wd33 {
  172. width: 33%;
  173. }
  174. .item {
  175. display: inline-block;
  176. text-align: center;
  177. padding: 0 30upx;
  178. height: 80rpx;
  179. line-height: 90upx;
  180. font-size: 32rpx;
  181. color: #A0A0A0;
  182. }
  183. .underline {
  184. height: 4upx;
  185. background-color: #000;
  186. border-radius: 3px;
  187. transition: .5s;
  188. }
  189. </style>