swiper-list.uvue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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">
  5. <text ref="swipertab" space="nbsp" class="swiper-tabs-item"
  6. :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''" v-for="(item, index) in swiperList" :key="index"
  7. @click="onTabClick(index)">
  8. {{item.title}}
  9. </text>
  10. </view>
  11. <view ref="indicator" class="swiper-tabs-indicator"></view>
  12. </scroll-view>
  13. <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
  14. @animationfinish="onSwiperAnimationfinish">
  15. <swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
  16. <text class="swiper-item-text">{{index}}</text>
  17. <!-- 可以将上述text组件替换为list组件,实现可左右滑动的长列表 -->
  18. </swiper-item>
  19. </swiper>
  20. </view>
  21. </template>
  22. <script>
  23. type SwiperTabsItem = {
  24. x : number,
  25. w : number
  26. }
  27. type SwiperViewItem = {
  28. title : string,
  29. }
  30. /**
  31. * 根据权重在两个值之间执行线性插值.
  32. * @constructor
  33. * @param {number} value1 - 第一个值,该值应为下限.
  34. * @param {number} value2 - 第二个值,该值应为上限.
  35. * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
  36. * @returns {number} 内插值
  37. */
  38. function lerpNumber(value1 : number, value2 : number, amount : number) : number {
  39. return (value1 + (value2 - value1) * amount)
  40. }
  41. export default {
  42. data() {
  43. return {
  44. swiperList: [] as SwiperViewItem[],
  45. swiperIndex: 0,
  46. tabScrollView: null as null | UniElement,
  47. indicatorNode: null as null | UniElement,
  48. animationFinishIndex: 0,
  49. swiperWidth: 0,
  50. swiperTabsRect: [] as SwiperTabsItem[]
  51. }
  52. },
  53. onLoad() {
  54. // 初始化 8 个页签,宽度依次递增,用于演示在滑动 swiper 过程中指示线宽度的线性变化
  55. for (let i = 0; i < 8; i++) {
  56. const space = " ".repeat(i)
  57. this.swiperList.push({
  58. title: "Tab " + space + i
  59. } as SwiperViewItem)
  60. }
  61. },
  62. onReady() {
  63. this.tabScrollView = this.$refs['tabScroll'] as UniElement;
  64. this.indicatorNode = this.$refs['indicator'] as UniElement;
  65. (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync()!.then((res : DOMRect) : Promise<void> => {
  66. this.swiperWidth = res.width
  67. return this.cacheTabItemsSize()
  68. }).then(() => {
  69. this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
  70. });
  71. },
  72. methods: {
  73. /**
  74. * 每个页签的点击事件,点击后设置当前 swiper.
  75. * @constructor
  76. * @param {number} index - 当前索引,该值应介于 0 和 最大数量之间.
  77. */
  78. onTabClick(index : number) {
  79. this.setSwiperIndex(index, false)
  80. },
  81. onSwiperTransition(e : SwiperTransitionEvent) {
  82. // 微信 skyline 每项完成触发 Animationfinish,偏移值重置
  83. // 微信 webview 全部完成触发 Animationfinish,偏移值累加
  84. // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
  85. // uni-app-x 和 微信 webview 行为一致
  86. const offset_x = e.detail.dx
  87. // 计算当前索引并重置差异
  88. const current_offset_x = offset_x % this.swiperWidth
  89. const current_offset_i = offset_x / this.swiperWidth
  90. const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
  91. // 计算目标索引及边界检查
  92. let move_to_index = current_index
  93. if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
  94. move_to_index += 1
  95. } else if (current_offset_x < 0 && move_to_index > 0) {
  96. move_to_index -= 1
  97. }
  98. // 计算偏移百分比
  99. const percentage = Math.abs(current_offset_x) / this.swiperWidth
  100. // 通知更新指示线
  101. if (current_index != move_to_index) {
  102. this.updateTabIndicator(current_index, move_to_index, percentage)
  103. }
  104. },
  105. /**
  106. * Swiper滑动动画结束事件.
  107. */
  108. onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
  109. this.setSwiperIndex(e.detail.current, true)
  110. // 记录上次的索引位置
  111. this.animationFinishIndex = e.detail.current
  112. },
  113. /**
  114. * 缓存所有页签的左边距和宽度,用于计算指示线在移动过程中的线性变化.
  115. */
  116. async cacheTabItemsSize() {
  117. this.swiperTabsRect.length = 0;
  118. const tabs = this.$refs["swipertab"] as UniElement[]
  119. for (let i = 0; i < tabs.length; i++) {
  120. const element = tabs[i];
  121. // #ifdef MP
  122. const rect = await element.getBoundingClientRectAsync()!
  123. const x = rect.left
  124. const w = rect.width
  125. // #endif
  126. // #ifndef MP
  127. const x = element.offsetLeft
  128. const w = element.offsetWidth
  129. // #endif
  130. this.swiperTabsRect.push({
  131. x,
  132. w
  133. } as SwiperTabsItem)
  134. }
  135. },
  136. /**
  137. * 设置当前 swiper 选中的索引值.
  138. * @constructor
  139. * @param {number} index - 当前索引,该值应介于 0 和 最大数量之间.
  140. * @param {boolean} updateIndicator - 是否更新指示线.
  141. */
  142. setSwiperIndex(index : number, updateIndicator : boolean) {
  143. if (this.swiperIndex === index) {
  144. return
  145. }
  146. this.swiperIndex = index
  147. if (updateIndicator) {
  148. this.updateTabIndicator(index, index, 1)
  149. }
  150. },
  151. /**
  152. * 更新页签指示线的位置和宽度.
  153. * @constructor
  154. * @param {number} current_index - 当前索引,该值应介于 0 和 最大数量之间.
  155. * @param {number} move_to_index - 目标索引,该值应介于 0 和 最大数量之间.
  156. * @param {number} percentage - 偏移百分比,应介于 0 和 1 之间,用于计算在移动过程中的线性值.
  157. */
  158. updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
  159. const current_size = this.swiperTabsRect[current_index]
  160. const move_to_size = this.swiperTabsRect[move_to_index]
  161. // 计算指示线 位置 和 宽度 在移动过程中的线性值
  162. const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
  163. const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
  164. // 通过 transform 更新指示线,避免重排版
  165. // #ifdef APP
  166. const x = indicator_line_x + indicator_line_w / 2
  167. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
  168. // #endif
  169. // #ifdef WEB || MP
  170. // TODO chrome windows系统 transform scaleX渲染bug
  171. const x = indicator_line_x
  172. this.indicatorNode?.style?.setProperty('width', `${indicator_line_w}px`)
  173. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px)`)
  174. // #endif
  175. // 滚动到水平中心位置
  176. const scroll_x = x - this.swiperWidth / 2
  177. // app 平台后续支持 scrollTo()
  178. // #ifndef MP-WEIXIN
  179. this.tabScrollView!.scrollLeft = scroll_x
  180. // #endif
  181. // #ifdef MP-WEIXIN
  182. this.tabScrollView!.scrollTo({
  183. left: scroll_x
  184. })
  185. // #endif
  186. }
  187. }
  188. }
  189. </script>
  190. <style>
  191. .flex-row {
  192. flex-direction: row;
  193. align-self: flex-start;
  194. }
  195. .swiper-list {
  196. flex: 1;
  197. }
  198. .swiper-tabs {
  199. background-color: #ffffff;
  200. }
  201. .swiper-tabs-item {
  202. color: #555;
  203. font-size: 16px;
  204. padding: 12px 25px;
  205. white-space: nowrap;
  206. }
  207. .swiper-tabs-item-active {
  208. color: #007AFF;
  209. }
  210. .swiper-tabs-indicator {
  211. width: 1px;
  212. height: 2px;
  213. background-color: #007AFF;
  214. }
  215. .swiper-view {
  216. flex: 1;
  217. }
  218. .swiper-item {
  219. flex: 1;
  220. align-items: center;
  221. justify-content: center;
  222. }
  223. .swiper-item-text {
  224. font-size: 72px;
  225. font-weight: bold;
  226. }
  227. </style>