long-list.uvue 5.6 KB
Newer Older
H
hdx 已提交
1
<template>
H
hdx 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  <scroll-view class="page" :scroll-top="pageScrollTop">
    <view class="search-bar">
      <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"
                :class="swiperIndex==index ? 'uni-tab-item-title-active' : ''">{{item.name}}</text>
            </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 25 26 27 28 29
      </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">
          <long-page :type="item.type"></long-page>
        </swiper-item>
      </swiper>
    </view>
  </scroll-view>
H
hdx 已提交
30 31 32
</template>

<script>
H
hdx 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  import longPage from './long-list-page.uvue';

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

  type SwiperViewItem = {
    type : string,
    name : string
  }

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

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

        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 }

        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);
        //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        this.$lastSwiperIndex = e.detail.current;
        // console.log("onSwiperAnimationfinish", e.detail.current);
        // this.setSwiperIndex(e.detail.current, true);
      },
      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 已提交
133 134
          return
        }
H
hdx 已提交
135
        this.swiperIndex = index
H
hdx 已提交
136

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

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

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

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

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

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

H
hdx 已提交
174 175
  .swiper-tabs-item {
    padding: 12px 25px;
H
hdx 已提交
176 177
  }

H
hdx 已提交
178 179 180
  .swiper-tabs-item-text {
    color: #555;
    font-size: 16px;
H
hdx 已提交
181 182
  }

H
hdx 已提交
183 184
  .swiper-tabs-item-text-active {
    color: #007AFF;
H
hdx 已提交
185 186
  }

H
hdx 已提交
187 188 189
  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
H
hdx 已提交
190 191
  }

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

H
hdx 已提交
200 201
  .swiper-view {
    flex: 1;
H
hdx 已提交
202 203
  }

H
hdx 已提交
204 205
  .swiper-item {
    flex: 1;
H
hdx 已提交
206 207
  }
</style>