long-list2.uvue 8.7 KB
Newer Older
1
<template>
雪洛's avatar
雪洛 已提交
2
  <scroll-view ref="pageScrollView" class="page" :rebound="false"
3 4
    @startnestedscroll="onStartNestedScroll" @nestedprescroll="onNestedPreScroll"
    :nested-scroll-child="nestedScrollChildId"
5
    >
shutao-dc's avatar
shutao-dc 已提交
6 7
    <swiper ref="header" indicator-dots="true" circular="true">
      <swiper-item  v-for="i in 3" :item-id="i">
8
        <image src="/static/shuijiao.jpg" style="width:100% ;height: 240px;"></image>
shutao-dc's avatar
shutao-dc 已提交
9 10
        <text style="position: absolute;">{{i}}</text>
      </swiper-item>
11 12
    </swiper>
    <view class="swiper-list">
雪洛's avatar
雪洛 已提交
13
      <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        <view class="flex-row" style="align-self: flex-start;">
          <text ref="swipertab" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
            v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)">
            {{item.name}}
          </text>
        </view>
        <view ref="indicator" class="swiper-tabs-indicator"></view>
      </scroll-view>
      <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
        @animationfinish="onSwiperAnimationfinish">
        <swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
          <long-page ref="longPage" :id="item.id" :type="item.type" :preload="item.preload"></long-page>
        </swiper-item>
      </swiper>
    </view>
  </scroll-view>
</template>

<script>
  import longPage from '../long-list/long-list-page.uvue';

  type SwiperTabsItem = {
    x : number,
    w : number
  }

  type SwiperViewItem = {
    type : string,
42
    name : string,
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    id: string,
    preload : boolean,
  }

  /**
   * 根据权重在两个值之间执行线性插值.
   * @constructor
   * @param {number} value1 - 第一个值,该值应为下限.
   * @param {number} value2 - 第二个值,该值应为上限.
   * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
   * @returns {number} 内插值
   */
  function lerpNumber(value1 : number, value2 : number, amount : number) : number {
    return (value1 + (value2 - value1) * amount)
  }

  export default {
    components: {
      longPage
    },
    data() {
      return {
        swiperList: [
          {
            type: 'UpdatedDate',
68
            name: '最新上架',
69 70 71 72 73
            id: "list-id-1",
            preload: true
          } as SwiperViewItem,
          {
            type: 'FreeHot',
74
            name: '免费热榜',
75 76 77 78 79
            id: "list-id-2",
            preload: false
          } as SwiperViewItem,
          {
            type: 'PaymentHot',
80
            name: '付费热榜',
81 82 83 84 85
            id: "list-id-3",
            preload: false
          } as SwiperViewItem,
          {
            type: 'HotList',
86
            name: '热门总榜',
87 88 89 90 91
            id: "list-id-4",
            preload: false
          } as SwiperViewItem
        ] as SwiperViewItem[],
        swiperIndex: -1,
雪洛's avatar
雪洛 已提交
92 93 94 95 96 97 98
        pageScrollView: null as null | UniElement,
        headerHeight: 0,
        animationFinishIndex: 0,
        tabScrollView: null as null | UniElement,
        indicatorNode: null as null | UniElement,
        swiperWidth: 0,
        swiperTabsRect: [] as SwiperTabsItem[],
99 100 101 102
        nestedScrollChildId: ""
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
103 104 105 106 107
      this.pageScrollView = this.$refs['pageScrollView'] as UniElement;
      this.headerHeight = (this.$refs['header'] as UniElement).offsetHeight
      this.swiperWidth = (this.$refs['swiper'] as UniElement).getBoundingClientRect().width
      this.tabScrollView = this.$refs['tabScroll'] as UniElement
      this.indicatorNode = this.$refs['indicator'] as UniElement
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      this.cacheTabItemsSize()
      this.setSwiperIndex(0, true)
    },
    onPullDownRefresh() {
      (this.$refs["longPage"]! as ComponentPublicInstance[])[this.swiperIndex].$callMethod('refreshData', () => {
        uni.stopPullDownRefresh()
      })
    },
    methods: {
      // TODO
      onStartNestedScroll(_ : StartNestedScrollEvent) : boolean {
        return true
      },
      onNestedPreScroll(event : NestedPreScrollEvent) {
        const deltaY = event.deltaY
雪洛's avatar
雪洛 已提交
123
        const scrollTop = this.pageScrollView!.scrollTop
124 125 126

        if (deltaY > 0) {
          // 向上滚动,如果父容器 header scrollTop < offsetHeight,先滚动父容器
雪洛's avatar
雪洛 已提交
127 128
          if (scrollTop < this.headerHeight) {
            const difference = this.headerHeight - scrollTop - deltaY
129
            if (difference > 0) {
雪洛's avatar
雪洛 已提交
130
              this.pageScrollView!.scrollBy(event.deltaX, deltaY)
131 132 133 134
              event.consumed(event.deltaX, deltaY)
            } else {
              const top : number = deltaY + difference
              event.consumed(event.deltaX, top.toFloat())
雪洛's avatar
雪洛 已提交
135
              this.pageScrollView!.scrollBy(event.deltaX, top.toFloat())
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
            }
          }
        }
      },
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
        // 微信 skyline 每项完成触发 Animationfinish,偏移值重置
        // 微信 webview 全部完成触发 Animationfinish,偏移值累加
        // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
        // uni-app-x 和微信 webview 行为一致

        const offset_x = e.detail.dx

        // 计算当前索引并重置差异
雪洛's avatar
雪洛 已提交
152 153 154
        const current_offset_x = offset_x % this.swiperWidth
        const current_offset_i = offset_x / this.swiperWidth
        const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
155 156 157 158 159 160 161 162 163 164

        // 计算目标索引及边界检查
        let move_to_index = current_index
        if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
          move_to_index += 1
        } else if (current_offset_x < 0 && move_to_index > 0) {
          move_to_index -= 1
        }

        // 计算偏移百分比
雪洛's avatar
雪洛 已提交
165
        const percentage = Math.abs(current_offset_x) / this.swiperWidth
166 167 168 169 170 171 172 173 174

        // 通知更新指示线
        this.updateTabIndicator(current_index, move_to_index, percentage)

        // 首次可见时初始化数据
        this.initSwiperItemData(move_to_index)
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        this.setSwiperIndex(e.detail.current, true)
雪洛's avatar
雪洛 已提交
175
        this.animationFinishIndex = e.detail.current
176 177
      },
      cacheTabItemsSize() {
雪洛's avatar
雪洛 已提交
178
        this.swiperTabsRect.length = 0
DCloud-yyl's avatar
DCloud-yyl 已提交
179
        const tabs = this.$refs["swipertab"] as UniElement[]
180
        tabs.forEach((node) => {
雪洛's avatar
雪洛 已提交
181
          this.swiperTabsRect.push({
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            x: node.offsetLeft,
            w: node.offsetWidth
          } as SwiperTabsItem)
        })
      },
      setSwiperIndex(index : number, updateIndicator : boolean) {
        if (this.swiperIndex === index) {
          return
        }

        this.swiperIndex = index

        this.initSwiperItemData(index)

        if (updateIndicator) {
          this.updateTabIndicator(index, index, 1)
        }
      },
      updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
雪洛's avatar
雪洛 已提交
201 202
        const current_size = this.swiperTabsRect[current_index]
        const move_to_size = this.swiperTabsRect[move_to_index]
203 204 205 206 207 208 209

        // 计算指示线 左边距 和 宽度 在移动过程中的线性值
        const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
        const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)

        // 更新指示线
        const x = indicator_line_x + indicator_line_w / 2
雪洛's avatar
雪洛 已提交
210
        this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
211 212

        // 滚动到水平中心位置
雪洛's avatar
雪洛 已提交
213 214 215
        const scroll_x = x - this.swiperWidth / 2
        if(this.tabScrollView !== null){
          this.tabScrollView!.scrollLeft = scroll_x
216 217 218 219 220 221
        }
      },
      initSwiperItemData(index : number) {
        if (!this.swiperList[index].preload) {
          this.swiperList[index].preload = true;
          (this.$refs["longPage"]! as ComponentPublicInstance[])[index].$callMethod('loadData', null)
222 223
        }
        //swiper页面切换需要重新赋值嵌套滚动子元素的id
shutao-dc's avatar
shutao-dc 已提交
224
        this.nestedScrollChildId = this.swiperList[index].id
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
      }
    }
  }
</script>

<style>
  .flex-row {
    flex-direction: row;
  }

  .page {
    flex: 1;
  }

  .search-bar {
    padding: 10px;
  }

  .swiper-list {
    height: 100%;
  }

  .swiper-tabs {
    background-color: #ffffff;
    flex-direction: column;
  }

  .swiper-tabs-item {
    color: #555;
    font-size: 16px;
    padding: 12px 25px;
  }

  .swiper-tabs-item-active {
    color: #007AFF;
  }

  .swiper-tabs-indicator {
    width: 1px;
    height: 2px;
    background-color: #007AFF;
  }

  .swiper-view {
    flex: 1;
  }

  .swiper-item {
    flex: 1;
  }
shutao-dc's avatar
shutao-dc 已提交
275
</style>