long-list.uvue 6.4 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 7 8 9
      <input placeholder="搜索..." />
    </view>
    <view class="swiper-list">
      <scroll-view class="swiper-tabs" :scroll-left="tabsScrollLeft" :scroll-x="true" :show-scrollbar="false">
        <view>
          <view class="flex-row">
10 11 12 13
            <text class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
              v-for="(item, index) in swiperList" ref="swipertab" :key="index" @click="onTabClick(index)">
              {{item.name}}
            </text>
H
hdx 已提交
14 15 16 17 18
          </view>
          <view class="swiper-tabs-indicator">
            <view class="swiper-tabs-underline"
              :style="{left: swiperIndicatorLineLeft + 'px', width: swiperIndicatorLineWidth + 'px'}"></view>
          </view>
H
hdx 已提交
19
        </view>
H
hdx 已提交
20
      </scroll-view>
21 22
      <swiper class="swiper-view" ref="swiper" :current="swiperIndex" @transition="onSwiperTransition"
        @animationfinish="onSwiperAnimationfinish">
H
hdx 已提交
23
        <swiper-item class="swiper-item" v-for="(item, index) in swiperList" :key="index">
H
hdx 已提交
24
          <long-page ref="longPage" :type="item.type" :preload="item.preload"></long-page>
H
hdx 已提交
25 26 27 28
        </swiper-item>
      </swiper>
    </view>
  </scroll-view>
H
hdx 已提交
29 30 31
</template>

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

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

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

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

H
hdx 已提交
98 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

H
hdx 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        // 边界检查
        let move_to_index = current_index
        if (current_offset_x > 0) {
          if (move_to_index < this.$swiperTabsRect.length - 1) {
            move_to_index += 1
          }
        } else if (current_offset_x < 0) {
          if (move_to_index > 0) {
            move_to_index -= 1
          }
        }

        // 计算蛇形线位置和大小
        const percentage = Math.abs(current_offset_x) / this.$swiperWidth
        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
121

H
hdx 已提交
122
        // 更新蛇形线位置和大小
123 124
        // 这代码写的不对,不能在拖动滚动中使用vue绑定样式,只能用ref+transform
        // this.updateTabIndicator(indicator_line_l, indicator_line_w)
125

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

149 150
        this.initSwiperItemPage(index)

H
hdx 已提交
151
        if (updateIndicator) {
H
hdx 已提交
152
          this.updateTabIndicator(this.$swiperTabsRect[index].x, this.$swiperTabsRect[index].w)
H
hdx 已提交
153
        }
154
      },
H
hdx 已提交
155 156 157
      updateTabIndicator(left : Number, width : Number) {
        this.swiperIndicatorLineLeft = left
        this.swiperIndicatorLineWidth = width
158 159 160 161 162 163
        this.tabsScrollLeft = left + width / 2 - this.$swiperWidth / 2
      },
      initSwiperItemPage(index : Number) {
        if (!this.swiperList[index].preload) {
          this.swiperList[index].preload = true;
          (this.$refs["longPage"]! as ComponentPublicInstance[])[index].$callMethod('loadData')
H
hdx 已提交
164
        }
H
hdx 已提交
165 166 167 168 169 170 171 172 173 174
      }
    }
  }
</script>

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

H
hdx 已提交
175
  .page {
H
hdx 已提交
176 177 178
    flex: 1;
  }

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

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

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

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

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

H
hdx 已提交
202 203 204
  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
H
hdx 已提交
205 206
  }

H
hdx 已提交
207 208 209 210 211 212
  .swiper-tabs-underline {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 0;
    background-color: #007AFF;
H
hdx 已提交
213 214
  }

H
hdx 已提交
215 216
  .swiper-view {
    flex: 1;
H
hdx 已提交
217 218
  }

H
hdx 已提交
219 220
  .swiper-item {
    flex: 1;
H
hdx 已提交
221
  }
222
</style>