long-list.uvue 6.0 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"></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 43
    name : string,
    init : Boolean,
H
hdx 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  }

  // 测试数据
  const CategoryData = [
    {
      type: 'UpdatedDate',
      name: '最新上架'
    } as SwiperViewItem,
    {
      type: 'FreeHot',
      name: '免费热榜'
    } as SwiperViewItem,
    {
      type: 'PaymentHot',
      name: '付费热榜'
    } as SwiperViewItem,
    {
      type: 'HotList',
      name: '热门总榜'
    } as SwiperViewItem
  ]
H
hdx 已提交
65 66

  export default {
H
hdx 已提交
67 68 69
    components: {
      longPage
    },
H
hdx 已提交
70 71
    data() {
      return {
H
hdx 已提交
72 73 74 75 76 77
        pageScrollTop: 0,
        swiperList: [] as SwiperViewItem[],
        swiperIndex: -1,
        tabsScrollLeft: 0,
        swiperIndicatorLineLeft: 0,
        swiperIndicatorLineWidth: 0,
H
hdx 已提交
78
        $headerHeight: 0,
H
hdx 已提交
79 80 81 82
        $lastSwiperIndex: 0,
        $swiperWidth: 0,
        $swiperTabsRect: [] as SwiperTabsItem[],
        $isTap: false
H
hdx 已提交
83 84 85
      }
    },
    onLoad() {
H
hdx 已提交
86 87 88 89 90
      CategoryData.forEach((item) => {
        this.swiperList.push(item)
      })
    },
    onReady() {
H
hdx 已提交
91
      this.$headerHeight = (this.$refs["header"] as INode)?.offsetHeight as number
H
hdx 已提交
92 93 94
      this.$swiperWidth = (this.$refs["swiper"] as INode)?.offsetWidth as number
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
95 96
    },
    methods: {
H
hdx 已提交
97 98 99 100 101 102 103
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperChange(e : SwiperChangeEvent) {
        this.setSwiperIndex(e.detail.current, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
104
        const offsetX = e.detail.dx
H
hdx 已提交
105 106 107 108 109

        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 }

110 111 112 113 114 115 116 117 118
        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 已提交
119 120 121 122 123

        //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        // console.log("onSwiperAnimationfinish", e.detail.current);
H
hdx 已提交
124 125
        this.$lastSwiperIndex = e.detail.current
        this.setSwiperIndex(e.detail.current, true)
H
hdx 已提交
126
      },
H
hdx 已提交
127 128 129 130 131 132 133 134 135 136 137 138
      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 已提交
139 140
          return
        }
H
hdx 已提交
141
        this.swiperIndex = index
H
hdx 已提交
142

143 144
        this.initSwiperItemPage(index)

H
hdx 已提交
145 146 147
        if (updateIndicator) {
          this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
        }
148 149 150 151 152 153
      },
      initSwiperItemPage(index : Number) {
        if (!this.swiperList[index].init) {
          this.swiperList[index].init = true;
          (this.$refs["longPage"] as ComponentPublicInstance[])[index].$callMethod('init')
        }
H
hdx 已提交
154 155 156 157 158 159 160 161
      },
      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 已提交
162 163 164 165 166 167 168 169 170 171
      }
    }
  }
</script>

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

H
hdx 已提交
172
  .page {
H
hdx 已提交
173 174 175
    flex: 1;
  }

H
hdx 已提交
176
  .search-bar {
H
hdx 已提交
177 178 179
    padding: 10px;
  }

H
hdx 已提交
180
  .swiper-list {
H
hdx 已提交
181 182
    flex: 1;
    /* height: 100%; */
H
hdx 已提交
183 184
  }

H
hdx 已提交
185 186
  .swiper-tabs {
    background-color: #ffffff;
H
hdx 已提交
187 188
  }

H
hdx 已提交
189 190
  .swiper-tabs-item {
    padding: 12px 25px;
H
hdx 已提交
191 192
  }

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

H
hdx 已提交
198 199
  .swiper-tabs-item-text-active {
    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>