long-list.uvue 5.7 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 35 36 37 38 39 40
  import longPage from './long-list-page.uvue';

  type SwiperTabsItem = {
    left : number,
    width : number
  }

  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() {
H
hdx 已提交
82
      this.$headerHeight = (this.$refs["header"] as INode)?.offsetHeight as number
H
hdx 已提交
83 84 85
      this.$swiperWidth = (this.$refs["swiper"] as INode)?.offsetWidth as number
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
86 87
    },
    methods: {
H
hdx 已提交
88 89 90 91
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
92
        const offsetX = e.detail.dx
93 94 95
        // 兼容微信 skyline 和 webview
        const offsetIndex = Math.abs(offsetX.toInt()) == this.$swiperWidth ? 0 : offsetX / this.$swiperWidth
        const currentIndex = this.$animationFinishIndex + Math.abs(offsetIndex.toInt())
H
hdx 已提交
96

97
        let moveToIndex = offsetX > 0 ? currentIndex + 1 : currentIndex - 1
H
hdx 已提交
98 99 100
        if (moveToIndex < 0) { moveToIndex = 0 }
        if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }

101
        const percentage = Math.abs(offsetX) / this.$swiperWidth
102
        const currentSize = this.$swiperTabsRect[currentIndex]
103 104 105 106 107 108 109
        const moveToSize = this.$swiperTabsRect[moveToIndex]
        const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
        const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage

        this.updateTabIndicator(indicatorlineL, indicatorlineW)

        this.initSwiperItemPage(moveToIndex)
H
hdx 已提交
110 111
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
H
hdx 已提交
112
        this.setSwiperIndex(e.detail.current, true)
113
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
114
      },
H
hdx 已提交
115
      queryTabItemsSize() {
116
        this.$swiperTabsRect.length = 0
H
hdx 已提交
117 118 119 120 121 122 123 124 125 126
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
            left: node.offsetLeft as number,
            width: node.offsetWidth as number
          } as SwiperTabsItem)
        })
      },
      setSwiperIndex(index : Number, updateIndicator : Boolean) {
        if (this.swiperIndex === index) {
H
hdx 已提交
127 128
          return
        }
H
hdx 已提交
129
        this.swiperIndex = index
H
hdx 已提交
130

131 132
        this.initSwiperItemPage(index)

H
hdx 已提交
133 134 135
        if (updateIndicator) {
          this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
        }
136
      },
H
hdx 已提交
137 138 139
      updateTabIndicator(left : Number, width : Number) {
        this.swiperIndicatorLineLeft = left
        this.swiperIndicatorLineWidth = width
140 141 142 143 144 145
        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 已提交
146
        }
H
hdx 已提交
147 148 149 150 151 152 153 154 155 156
      }
    }
  }
</script>

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

H
hdx 已提交
157
  .page {
H
hdx 已提交
158 159 160
    flex: 1;
  }

H
hdx 已提交
161
  .search-bar {
H
hdx 已提交
162 163 164
    padding: 10px;
  }

H
hdx 已提交
165
  .swiper-list {
H
hdx 已提交
166 167
    flex: 1;
    /* height: 100%; */
H
hdx 已提交
168 169
  }

H
hdx 已提交
170 171
  .swiper-tabs {
    background-color: #ffffff;
H
hdx 已提交
172 173
  }

H
hdx 已提交
174 175 176
  .swiper-tabs-item {
    color: #555;
    font-size: 16px;
177
    padding: 12px 25px;
H
hdx 已提交
178 179
  }

180
  .swiper-tabs-item-active {
H
hdx 已提交
181
    color: #007AFF;
H
hdx 已提交
182 183
  }

H
hdx 已提交
184 185 186
  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
H
hdx 已提交
187 188
  }

H
hdx 已提交
189 190 191 192 193 194
  .swiper-tabs-underline {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 0;
    background-color: #007AFF;
H
hdx 已提交
195 196
  }

H
hdx 已提交
197 198
  .swiper-view {
    flex: 1;
H
hdx 已提交
199 200
  }

H
hdx 已提交
201 202
  .swiper-item {
    flex: 1;
H
hdx 已提交
203 204
  }
</style>