long-list.uvue 9.5 KB

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