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

<script>
H
hdx 已提交
33 34 35 36 37 38 39 40 41
  import longPage from './long-list-page.uvue';

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

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

H
hdx 已提交
46
  export default {
H
hdx 已提交
47 48 49
    components: {
      longPage
    },
H
hdx 已提交
50 51
    data() {
      return {
H
hdx 已提交
52
        pageScrollTop: 0,
53 54 55
        swiperList: [
          {
            type: 'UpdatedDate',
H
hdx 已提交
56 57
            name: '最新上架',
            preload: true
58 59 60 61 62 63 64 65 66 67 68 69 70 71
          } as SwiperViewItem,
          {
            type: 'FreeHot',
            name: '免费热榜'
          } as SwiperViewItem,
          {
            type: 'PaymentHot',
            name: '付费热榜'
          } as SwiperViewItem,
          {
            type: 'HotList',
            name: '热门总榜'
          } as SwiperViewItem
        ] as SwiperViewItem[],
H
hdx 已提交
72 73 74 75
        swiperIndex: -1,
        tabsScrollLeft: 0,
        swiperIndicatorLineLeft: 0,
        swiperIndicatorLineWidth: 0,
H
hdx 已提交
76
        $headerHeight: 0,
H
hdx 已提交
77 78 79 80
        $lastSwiperIndex: 0,
        $swiperWidth: 0,
        $swiperTabsRect: [] as SwiperTabsItem[],
        $isTap: false
H
hdx 已提交
81 82
      }
    },
H
hdx 已提交
83
    onReady() {
H
hdx 已提交
84
      this.$headerHeight = (this.$refs["header"] as INode)?.offsetHeight as number
H
hdx 已提交
85 86 87
      this.$swiperWidth = (this.$refs["swiper"] as INode)?.offsetWidth as number
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
88 89
    },
    methods: {
H
hdx 已提交
90 91 92 93 94 95 96
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperChange(e : SwiperChangeEvent) {
        this.setSwiperIndex(e.detail.current, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
97
        const offsetX = e.detail.dx
H
hdx 已提交
98 99 100 101 102

        let moveToIndex = offsetX > 0 ? this.$lastSwiperIndex + 1 : this.$lastSwiperIndex - 1
        if (moveToIndex < 0) { moveToIndex = 0 }
        if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }

103 104 105 106 107 108 109 110 111
        const percentage = Math.abs(offsetX) / this.$swiperWidth
        const currentSize = this.$swiperTabsRect[this.$lastSwiperIndex]
        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 已提交
112 113 114 115 116

        //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        // console.log("onSwiperAnimationfinish", e.detail.current);
H
hdx 已提交
117 118
        this.$lastSwiperIndex = e.detail.current
        this.setSwiperIndex(e.detail.current, true)
H
hdx 已提交
119
      },
H
hdx 已提交
120 121 122 123 124 125 126 127 128 129 130 131
      queryTabItemsSize() {
        this.$swiperTabsRect.length = 0;
        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 已提交
132 133
          return
        }
H
hdx 已提交
134
        this.swiperIndex = index
H
hdx 已提交
135

136 137
        this.initSwiperItemPage(index)

H
hdx 已提交
138 139 140
        if (updateIndicator) {
          this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
        }
141 142
      },
      initSwiperItemPage(index : Number) {
H
hdx 已提交
143 144 145
        if (!this.swiperList[index].preload) {
          this.swiperList[index].preload = true;
          (this.$refs["longPage"] as ComponentPublicInstance[])[index].$callMethod('loadData')
146
        }
H
hdx 已提交
147 148 149 150 151 152 153 154
      },
      updateTabIndicator(left : Number, width : Number) {
        this.swiperIndicatorLineLeft = left
        this.swiperIndicatorLineWidth = width
        const offset = left + width / 2
        if (offset > this.$swiperWidth / 2) {
          this.tabsScrollLeft = offset - this.$swiperWidth / 2
        }
H
hdx 已提交
155 156 157 158 159 160 161 162 163 164
      }
    }
  }
</script>

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

H
hdx 已提交
165
  .page {
H
hdx 已提交
166 167 168
    flex: 1;
  }

H
hdx 已提交
169
  .search-bar {
H
hdx 已提交
170 171 172
    padding: 10px;
  }

H
hdx 已提交
173
  .swiper-list {
H
hdx 已提交
174 175
    flex: 1;
    /* height: 100%; */
H
hdx 已提交
176 177
  }

H
hdx 已提交
178 179
  .swiper-tabs {
    background-color: #ffffff;
H
hdx 已提交
180 181
  }

H
hdx 已提交
182 183
  .swiper-tabs-item {
    padding: 12px 25px;
H
hdx 已提交
184 185
  }

H
hdx 已提交
186 187 188
  .swiper-tabs-item-text {
    color: #555;
    font-size: 16px;
H
hdx 已提交
189 190
  }

H
hdx 已提交
191 192
  .swiper-tabs-item-text-active {
    color: #007AFF;
H
hdx 已提交
193 194
  }

H
hdx 已提交
195 196 197
  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
H
hdx 已提交
198 199
  }

H
hdx 已提交
200 201 202 203 204 205
  .swiper-tabs-underline {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 0;
    background-color: #007AFF;
H
hdx 已提交
206 207
  }

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

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