long-list.uvue 6.3 KB
Newer Older
H
hdx 已提交
1
<template>
H
hdx 已提交
2
  <scroll-view class="page" :scroll-top="pageScrollTop">
H
hdx 已提交
3
    <view class="search-bar" ref="header">
H
hdx 已提交
4 5 6
      <input placeholder="搜索..." />
    </view>
    <view class="swiper-list">
7 8
      <scroll-view ref="tabScroll" class="swiper-tabs" :scroll-x="true" :show-scrollbar="false">
        <!-- TODO 多一层 view -->
H
hdx 已提交
9 10
        <view>
          <view class="flex-row">
11 12
            <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)">
13 14
              {{item.name}}
            </text>
H
hdx 已提交
15
          </view>
16
          <view ref="indicator" class="swiper-tabs-indicator"></view>
H
hdx 已提交
17
        </view>
H
hdx 已提交
18
      </scroll-view>
19
      <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
20
        @animationfinish="onSwiperAnimationfinish">
H
hdx 已提交
21
        <swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
H
hdx 已提交
22
          <long-page ref="longPage" :type="item.type" :preload="item.preload"></long-page>
H
hdx 已提交
23 24 25 26
        </swiper-item>
      </swiper>
    </view>
  </scroll-view>
H
hdx 已提交
27 28 29
</template>

<script>
H
hdx 已提交
30 31 32
  import longPage from './long-list-page.uvue';

  type SwiperTabsItem = {
H
hdx 已提交
33 34
    x : number,
    w : number
H
hdx 已提交
35 36 37 38
  }

  type SwiperViewItem = {
    type : string,
39
    name : string,
H
hdx 已提交
40
    preload : Boolean,
H
hdx 已提交
41 42
  }

H
hdx 已提交
43
  export default {
H
hdx 已提交
44 45 46
    components: {
      longPage
    },
H
hdx 已提交
47 48
    data() {
      return {
H
hdx 已提交
49
        pageScrollTop: 0,
50 51 52
        swiperList: [
          {
            type: 'UpdatedDate',
H
hdx 已提交
53 54
            name: '最新上架',
            preload: true
55 56 57 58 59 60 61 62 63 64 65 66 67 68
          } as SwiperViewItem,
          {
            type: 'FreeHot',
            name: '免费热榜'
          } as SwiperViewItem,
          {
            type: 'PaymentHot',
            name: '付费热榜'
          } as SwiperViewItem,
          {
            type: 'HotList',
            name: '热门总榜'
          } as SwiperViewItem
        ] as SwiperViewItem[],
H
hdx 已提交
69
        swiperIndex: -1,
70 71
        $tabScrollView: null as null | INode,
        $indicatorNode: null as null | INode,
H
hdx 已提交
72
        $headerHeight: 0,
73
        $animationFinishIndex: 0,
H
hdx 已提交
74
        $swiperWidth: 0,
75
        $swiperTabsRect: [] as SwiperTabsItem[]
H
hdx 已提交
76 77
      }
    },
H
hdx 已提交
78
    onReady() {
79 80
      this.$tabScrollView = this.$refs.get('tabScroll') as INode
      this.$indicatorNode = this.$refs.get('indicator') as INode
81
      this.$headerHeight = (this.$refs["header"] as INode).offsetHeight
H
hdx 已提交
82
      this.$swiperWidth = (this.$refs["swiper"] as INode).getBoundingClientRect().width
H
hdx 已提交
83 84
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
85 86
    },
    methods: {
H
hdx 已提交
87 88 89 90
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
91 92 93 94 95
        // 微信 skyline 每项完成触发 Animationfinish,偏移值重置
        // 微信 webview 全部完成触发 Animationfinish,偏移值累加
        // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
        // uni-app-x 微信 webview 行为一致

H
hdx 已提交
96
        const offset_x = e.detail.dx
H
hdx 已提交
97

98
        // 重置差异,计算当前索引
H
hdx 已提交
99 100 101
        const current_offset_x = offset_x % this.$swiperWidth
        const current_offset_i = offset_x / this.$swiperWidth
        const current_index = this.$animationFinishIndex + current_offset_i.toInt()
H
hdx 已提交
102

103
        // 更新索引及边界检查
H
hdx 已提交
104
        let move_to_index = current_index
105 106 107 108
        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
H
hdx 已提交
109 110
        }

111
        // 计算偏移百分比
H
hdx 已提交
112
        const percentage = Math.abs(current_offset_x) / this.$swiperWidth
113

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

H
hdx 已提交
117 118
        // 首次可见时初始化数据
        this.initSwiperItemPage(move_to_index)
H
hdx 已提交
119 120
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
H
hdx 已提交
121
        this.setSwiperIndex(e.detail.current, true)
122
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
123
      },
H
hdx 已提交
124
      queryTabItemsSize() {
125
        this.$swiperTabsRect.length = 0
H
hdx 已提交
126 127 128
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
H
hdx 已提交
129 130
            x: node.offsetLeft,
            w: node.offsetWidth
H
hdx 已提交
131 132 133 134 135
          } as SwiperTabsItem)
        })
      },
      setSwiperIndex(index : Number, updateIndicator : Boolean) {
        if (this.swiperIndex === index) {
H
hdx 已提交
136 137
          return
        }
138

H
hdx 已提交
139
        this.swiperIndex = index
H
hdx 已提交
140

141 142
        this.initSwiperItemPage(index)

H
hdx 已提交
143
        if (updateIndicator) {
144
          this.updateTabIndicator(index, index, 1)
H
hdx 已提交
145
        }
146
      },
147 148 149 150 151 152 153 154 155 156 157 158 159 160
      updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
        // 计算指示线
        const current_size = this.$swiperTabsRect[current_index]
        const move_to_size = this.$swiperTabsRect[move_to_index]
        const indicator_line_l = current_size.x + (move_to_size.x - current_size.x) * percentage
        const indicator_line_w = current_size.w + (move_to_size.w - current_size.w) * percentage

        // 更新指示线
        const x = indicator_line_l + indicator_line_w / 2
        this.$indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)

        // 将指示线滚动到水平中心位置
        const scroll_x = x - this.$swiperWidth / 2
        this.$tabScrollView?.setAttribute('scrollLeft', scroll_x)
161 162 163 164 165
      },
      initSwiperItemPage(index : Number) {
        if (!this.swiperList[index].preload) {
          this.swiperList[index].preload = true;
          (this.$refs["longPage"]! as ComponentPublicInstance[])[index].$callMethod('loadData')
H
hdx 已提交
166
        }
H
hdx 已提交
167 168 169 170 171 172 173 174 175 176
      }
    }
  }
</script>

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

H
hdx 已提交
177
  .page {
H
hdx 已提交
178 179 180
    flex: 1;
  }

H
hdx 已提交
181
  .search-bar {
H
hdx 已提交
182 183 184
    padding: 10px;
  }

H
hdx 已提交
185
  .swiper-list {
H
hdx 已提交
186 187
    flex: 1;
    /* height: 100%; */
H
hdx 已提交
188 189
  }

H
hdx 已提交
190 191
  .swiper-tabs {
    background-color: #ffffff;
H
hdx 已提交
192 193
  }

H
hdx 已提交
194 195 196
  .swiper-tabs-item {
    color: #555;
    font-size: 16px;
197
    padding: 12px 25px;
H
hdx 已提交
198 199
  }

200
  .swiper-tabs-item-active {
H
hdx 已提交
201
    color: #007AFF;
H
hdx 已提交
202 203
  }

H
hdx 已提交
204
  .swiper-tabs-indicator {
205
    width: 1px;
H
hdx 已提交
206 207
    height: 2px;
    background-color: #007AFF;
H
hdx 已提交
208 209
  }

H
hdx 已提交
210 211
  .swiper-view {
    flex: 1;
H
hdx 已提交
212 213
  }

H
hdx 已提交
214 215
  .swiper-item {
    flex: 1;
H
hdx 已提交
216
  }
217
</style>