long-list2.uvue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <scroll-view ref="pageScrollView" class="page" :bounces="false"
  3. @startnestedscroll="onStartNestedScroll" @nestedprescroll="onNestedPreScroll"
  4. :nested-scroll-child="nestedScrollChildId"
  5. >
  6. <swiper ref="header" indicator-dots="true" circular="true">
  7. <swiper-item v-for="i in 3" :item-id="i">
  8. <image src="/static/shuijiao.jpg" style="width:100% ;height: 240px;"></image>
  9. </swiper-item>
  10. </swiper>
  11. <view class="swiper-list">
  12. <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
  13. <view class="flex-row" style="align-self: flex-start;">
  14. <text ref="swipertab" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
  15. v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)">
  16. {{item.name}}
  17. </text>
  18. </view>
  19. <view ref="indicator" class="swiper-tabs-indicator"></view>
  20. </scroll-view>
  21. <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
  22. @animationfinish="onSwiperAnimationfinish">
  23. <swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
  24. <long-page ref="longPage" :id="item.id" :type="item.type" :preload="item.preload"></long-page>
  25. </swiper-item>
  26. </swiper>
  27. </view>
  28. </scroll-view>
  29. </template>
  30. <script>
  31. import longPage from '../long-list/long-list-page.uvue';
  32. type SwiperTabsItem = {
  33. x : number,
  34. w : number
  35. }
  36. type SwiperViewItem = {
  37. type : string,
  38. name : string,
  39. id: string,
  40. preload : boolean,
  41. }
  42. /**
  43. * 根据权重在两个值之间执行线性插值.
  44. * @constructor
  45. * @param {number} value1 - 第一个值,该值应为下限.
  46. * @param {number} value2 - 第二个值,该值应为上限.
  47. * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
  48. * @returns {number} 内插值
  49. */
  50. function lerpNumber(value1 : number, value2 : number, amount : number) : number {
  51. return (value1 + (value2 - value1) * amount)
  52. }
  53. export default {
  54. components: {
  55. longPage
  56. },
  57. data() {
  58. return {
  59. swiperList: [
  60. {
  61. type: 'UpdatedDate',
  62. name: '最新上架',
  63. id: "list-id-1",
  64. preload: true
  65. } as SwiperViewItem,
  66. {
  67. type: 'FreeHot',
  68. name: '免费热榜',
  69. id: "list-id-2",
  70. preload: false
  71. } as SwiperViewItem,
  72. {
  73. type: 'PaymentHot',
  74. name: '付费热榜',
  75. id: "list-id-3",
  76. preload: false
  77. } as SwiperViewItem,
  78. {
  79. type: 'HotList',
  80. name: '热门总榜',
  81. id: "list-id-4",
  82. preload: false
  83. } as SwiperViewItem
  84. ] as SwiperViewItem[],
  85. swiperIndex: 0,
  86. pageScrollView: null as null | UniElement,
  87. headerHeight: 0,
  88. animationFinishIndex: 0,
  89. tabScrollView: null as null | UniElement,
  90. indicatorNode: null as null | UniElement,
  91. swiperWidth: 0,
  92. swiperTabsRect: [] as SwiperTabsItem[],
  93. nestedScrollChildId: ""
  94. }
  95. },
  96. onReady() {
  97. this.pageScrollView = this.$refs['pageScrollView'] as UniElement;
  98. this.headerHeight = (this.$refs['header'] as UniElement).offsetHeight;
  99. this.tabScrollView = this.$refs['tabScroll'] as UniElement;
  100. this.indicatorNode = this.$refs['indicator'] as UniElement;
  101. (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync()!.then((res : DOMRect) : Promise<void> => {
  102. this.swiperWidth = res.width
  103. return this.cacheTabItemsSize()
  104. }).then(() => {
  105. this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
  106. });
  107. },
  108. onPullDownRefresh() {
  109. (this.$refs["longPage"]! as ComponentPublicInstance[])[this.swiperIndex].$callMethod('refreshData', () => {
  110. uni.stopPullDownRefresh()
  111. })
  112. },
  113. methods: {
  114. // TODO
  115. onStartNestedScroll(_ : StartNestedScrollEvent) : boolean {
  116. return true
  117. },
  118. onNestedPreScroll(event : NestedPreScrollEvent) {
  119. const deltaY = event.deltaY
  120. const scrollTop = this.pageScrollView!.scrollTop
  121. if (deltaY > 0) {
  122. // 向上滚动,如果父容器 header scrollTop < offsetHeight,先滚动父容器
  123. if (scrollTop < this.headerHeight) {
  124. const difference = this.headerHeight - scrollTop - deltaY
  125. if (difference > 0) {
  126. this.pageScrollView!.scrollBy(event.deltaX, deltaY)
  127. event.consumed(event.deltaX, deltaY)
  128. } else {
  129. const top : number = deltaY + difference
  130. event.consumed(event.deltaX, top.toFloat())
  131. this.pageScrollView!.scrollBy(event.deltaX, top.toFloat())
  132. }
  133. }
  134. }
  135. },
  136. onTabClick(index : number) {
  137. this.setSwiperIndex(index, false)
  138. },
  139. onSwiperTransition(e : SwiperTransitionEvent) {
  140. // 微信 skyline 每项完成触发 Animationfinish,偏移值重置
  141. // 微信 webview 全部完成触发 Animationfinish,偏移值累加
  142. // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
  143. // uni-app-x 和微信 webview 行为一致
  144. const offset_x = e.detail.dx
  145. // 计算当前索引并重置差异
  146. const current_offset_x = offset_x % this.swiperWidth
  147. const current_offset_i = offset_x / this.swiperWidth
  148. const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
  149. // 计算目标索引及边界检查
  150. let move_to_index = current_index
  151. if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
  152. move_to_index += 1
  153. } else if (current_offset_x < 0 && move_to_index > 0) {
  154. move_to_index -= 1
  155. }
  156. // 计算偏移百分比
  157. const percentage = Math.abs(current_offset_x) / this.swiperWidth
  158. // 通知更新指示线
  159. if (current_index != move_to_index) {
  160. this.updateTabIndicator(current_index, move_to_index, percentage)
  161. }
  162. // 首次可见时初始化数据
  163. this.initSwiperItemData(move_to_index)
  164. },
  165. onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
  166. this.setSwiperIndex(e.detail.current, true)
  167. this.animationFinishIndex = e.detail.current
  168. },
  169. async cacheTabItemsSize() {
  170. this.swiperTabsRect.length = 0;
  171. const tabs = this.$refs["swipertab"] as UniElement[]
  172. for (let i = 0; i < tabs.length; i++) {
  173. const element = tabs[i];
  174. // #ifdef MP
  175. const rect = await element.getBoundingClientRectAsync()!
  176. const x = rect.left
  177. const w = rect.width
  178. // #endif
  179. // #ifndef MP
  180. const x = element.offsetLeft
  181. const w = element.offsetWidth
  182. // #endif
  183. this.swiperTabsRect.push({
  184. x,
  185. w
  186. } as SwiperTabsItem)
  187. }
  188. },
  189. setSwiperIndex(index : number, updateIndicator : boolean) {
  190. if (this.swiperIndex === index) {
  191. return
  192. }
  193. this.swiperIndex = index
  194. this.initSwiperItemData(index)
  195. if (updateIndicator) {
  196. this.updateTabIndicator(index, index, 1)
  197. }
  198. },
  199. updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
  200. const current_size = this.swiperTabsRect[current_index]
  201. const move_to_size = this.swiperTabsRect[move_to_index]
  202. // 计算指示线 左边距 和 宽度 在移动过程中的线性值
  203. const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
  204. const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
  205. // 更新指示线
  206. // #ifdef APP
  207. const x = indicator_line_x + indicator_line_w / 2
  208. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
  209. this.initSwiperItemData(current_index)
  210. // #endif
  211. // #ifdef WEB || MP
  212. // TODO chrome windows系统 transform scaleX渲染bug
  213. const x = indicator_line_x
  214. this.indicatorNode?.style?.setProperty('width', `${indicator_line_w}px`)
  215. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px)`)
  216. // #endif
  217. // 滚动到水平中心位置
  218. const scroll_x = x - this.swiperWidth / 2
  219. // app 平台后续支持 scrollTo()
  220. // #ifndef MP-WEIXIN
  221. this.tabScrollView!.scrollLeft = scroll_x
  222. // #endif
  223. // #ifdef MP-WEIXIN
  224. this.tabScrollView!.scrollTo({
  225. left: scroll_x
  226. })
  227. // #endif
  228. },
  229. initSwiperItemData(index : number) {
  230. if (!this.swiperList[index].preload) {
  231. this.swiperList[index].preload = true;
  232. (this.$refs["longPage"]! as ComponentPublicInstance[])[index].$callMethod('loadData', null)
  233. }
  234. //swiper页面切换需要重新赋值嵌套滚动子元素的id
  235. this.nestedScrollChildId = this.swiperList[index].id
  236. }
  237. }
  238. }
  239. </script>
  240. <style>
  241. .flex-row {
  242. flex-direction: row;
  243. }
  244. .page {
  245. flex: 1;
  246. }
  247. .search-bar {
  248. padding: 10px;
  249. }
  250. .swiper-list {
  251. height: 100%;
  252. /* #ifdef WEB */
  253. flex: 1;
  254. /* #endif */
  255. }
  256. .swiper-tabs {
  257. background-color: #ffffff;
  258. flex-direction: column;
  259. }
  260. .swiper-tabs-item {
  261. color: #555;
  262. font-size: 16px;
  263. padding: 12px 25px;
  264. white-space: nowrap;
  265. }
  266. .swiper-tabs-item-active {
  267. color: #007AFF;
  268. }
  269. .swiper-tabs-indicator {
  270. width: 1px;
  271. height: 2px;
  272. background-color: #007AFF;
  273. }
  274. .swiper-view {
  275. flex: 1;
  276. }
  277. .swiper-item {
  278. flex: 1;
  279. }
  280. </style>