swiper-list2.uvue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="swiper-list">
  3. <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
  4. <view class="flex-row" style="align-self: flex-start;">
  5. <text ref="swipertab" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
  6. v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)">
  7. {{item.title}}
  8. </text>
  9. </view>
  10. </scroll-view>
  11. <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
  12. @animationfinish="onSwiperAnimationfinish">
  13. <swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
  14. <text class="swiper-item-text">{{index}}</text>
  15. <!-- 可以将上述text组件替换为list组件,实现可左右滑动的长列表 -->
  16. </swiper-item>
  17. </swiper>
  18. </view>
  19. </template>
  20. <script>
  21. type SwiperViewItem = {
  22. title : string,
  23. }
  24. /**
  25. * 根据权重在两个值之间执行线性插值.
  26. * @constructor
  27. * @param {number} value1 - 第一个值,该值应为下限.
  28. * @param {number} value2 - 第二个值,该值应为上限.
  29. * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
  30. * @returns {number} 内插值
  31. */
  32. function lerpNumber(value1 : number, value2 : number, amount : number) : number {
  33. return (value1 + (value2 - value1) * amount)
  34. }
  35. export default {
  36. data() {
  37. return {
  38. swiperList: [] as SwiperViewItem[],
  39. swiperIndex: 0,
  40. tabScrollView: null as null | UniElement,
  41. animationFinishIndex: 0,
  42. swiperWidth: 0
  43. }
  44. },
  45. onLoad() {
  46. for (let i = 0; i < 4; i++) {
  47. this.swiperList.push({
  48. title: "Tab " + i
  49. } as SwiperViewItem)
  50. }
  51. },
  52. onReady() {
  53. this.tabScrollView = this.$refs['tabScroll'] as UniElement;
  54. (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync()!.then((res : DOMRect) => {
  55. this.swiperWidth = res.width
  56. this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
  57. });
  58. },
  59. methods: {
  60. onTabClick(index : number) {
  61. this.setSwiperIndex(index, false)
  62. },
  63. onSwiperTransition(e : SwiperTransitionEvent) {
  64. const offset_x = e.detail.dx
  65. // 计算当前索引并重置差异
  66. const current_offset_x = offset_x % this.swiperWidth
  67. const current_offset_i = offset_x / this.swiperWidth
  68. const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
  69. // 计算目标索引及边界检查
  70. let move_to_index = current_index
  71. if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
  72. move_to_index += 1
  73. } else if (current_offset_x < 0 && move_to_index > 0) {
  74. move_to_index -= 1
  75. }
  76. // 计算偏移百分比
  77. const percentage = Math.abs(current_offset_x) / this.swiperWidth
  78. // 通知更新指示线
  79. if (current_index != move_to_index) {
  80. this.updateTabIndicator(current_index, move_to_index, percentage)
  81. }
  82. },
  83. onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
  84. this.setSwiperIndex(e.detail.current, true)
  85. this.animationFinishIndex = e.detail.current
  86. },
  87. setSwiperIndex(index : number, updateIndicator : boolean) {
  88. if (this.swiperIndex == index) {
  89. return
  90. }
  91. this.swiperIndex = index
  92. if (updateIndicator) {
  93. this.updateTabIndicator(index, index, 1)
  94. }
  95. },
  96. updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
  97. if (percentage == 0) {
  98. return
  99. }
  100. // 缩放范围
  101. const min_ratio = 1
  102. const max_ratio = 1.3
  103. const tabs = this.$refs['swipertab'] as UniElement[]
  104. const current_node = tabs[current_index]
  105. const move_to_node = tabs[move_to_index]
  106. // 当前
  107. const current_scale = lerpNumber(min_ratio, max_ratio, 1 - percentage)
  108. current_node.style.setProperty('transform', `scale(${current_scale})`)
  109. // 目标
  110. const move_to_scale = lerpNumber(min_ratio, max_ratio, percentage)
  111. move_to_node.style.setProperty('transform', `scale(${move_to_scale})`)
  112. // 滚动到水平中心位置
  113. const target_x = lerpNumber(current_node.offsetLeft, move_to_node.offsetLeft, percentage)
  114. const center_x = target_x + move_to_node.offsetWidth / 2 - this.swiperWidth / 2
  115. // app 平台后续支持 scrollTo()
  116. // #ifndef MP-WEIXIN
  117. this.tabScrollView!.scrollLeft = center_x
  118. // #endif
  119. // #ifdef MP-WEIXIN
  120. this.tabScrollView!.scrollTo({
  121. left: center_x
  122. })
  123. // #endif
  124. }
  125. }
  126. }
  127. </script>
  128. <style>
  129. .flex-row {
  130. flex-direction: row;
  131. }
  132. .swiper-list {
  133. flex: 1;
  134. }
  135. .swiper-tabs-item {
  136. color: #555;
  137. font-size: 16px;
  138. margin: 15px 25px 5px 25px;
  139. white-space: nowrap;
  140. }
  141. .swiper-tabs-item-active {
  142. color: #000000;
  143. }
  144. .swiper-view {
  145. flex: 1;
  146. }
  147. .swiper-item {
  148. flex: 1;
  149. align-items: center;
  150. justify-content: center;
  151. }
  152. .swiper-item-text {
  153. font-size: 72px;
  154. font-weight: bold;
  155. }
  156. </style>