swiper-list.uvue 4.7 KB
Newer Older
H
hdx 已提交
1 2 3 4 5
<template>
  <view class="swiper-list">
    <scroll-view class="swiper-tabs" :scroll-left="tabsScrollLeft" :scroll-x="true" :show-scrollbar="false">
      <view>
        <view class="flex-row">
6 7 8 9
          <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.title}}
          </text>
H
hdx 已提交
10 11 12 13 14 15 16
        </view>
        <view class="swiper-tabs-indicator">
          <view class="swiper-tabs-underline"
            :style="{left: swiperIndicatorLineLeft + 'px', width: swiperIndicatorLineWidth + 'px'}"></view>
        </view>
      </view>
    </scroll-view>
17 18
    <swiper class="swiper-view" ref="swiper" :current="swiperIndex" @transition="onSwiperTransition"
      @animationfinish="onSwiperAnimationfinish">
H
hdx 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32
      <swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
        <text class="swiper-item-text">{{index}}</text>
      </swiper-item>
    </swiper>
  </view>
</template>

<script>
  type SwiperTabsItem = {
    left : number,
    width : number
  }

  type SwiperViewItem = {
33
    title : string,
H
hdx 已提交
34 35 36 37 38 39 40 41 42 43
  }

  export default {
    data() {
      return {
        swiperList: [] as SwiperViewItem[],
        swiperIndex: -1,
        tabsScrollLeft: 0,
        swiperIndicatorLineLeft: 0,
        swiperIndicatorLineWidth: 0,
44
        $animationFinishIndex: 0,
H
hdx 已提交
45
        $swiperWidth: 0,
46
        $swiperTabsRect: [] as SwiperTabsItem[]
H
hdx 已提交
47 48 49 50
      }
    },
    onLoad() {
      for (let i = 0; i < 8; i++) {
51
        const space = " ".repeat(i)
H
hdx 已提交
52
        this.swiperList.push({
53
          title: "Tab " + space + i
H
hdx 已提交
54 55 56 57
        } as SwiperViewItem)
      }
    },
    onReady() {
58 59 60
      this.$swiperWidth = (this.$refs["swiper"] as INode)!.offsetWidth! as number
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
61 62 63
    },
    methods: {
      onTabClick(index : number) {
64
        this.setSwiperIndex(index, false)
H
hdx 已提交
65 66
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
67 68 69 70
        const offsetX = e.detail.dx
        // 兼容微信 skyline 和 webview
        const offsetIndex = Math.abs(offsetX.toInt()) == this.$swiperWidth ? 0 : offsetX / this.$swiperWidth
        const currentIndex = this.$animationFinishIndex + Math.abs(offsetIndex.toInt())
H
hdx 已提交
71

72
        let moveToIndex = offsetX > 0 ? currentIndex + 1 : currentIndex - 1
H
hdx 已提交
73 74 75
        if (moveToIndex < 0) { moveToIndex = 0 }
        if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }

76 77 78 79 80
        const percentage = Math.abs(offsetX) / this.$swiperWidth
        const currentSize = this.$swiperTabsRect[currentIndex]
        const moveToSize = this.$swiperTabsRect[moveToIndex]
        const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage
        const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage
H
hdx 已提交
81

82
        this.updateTabIndicator(indicatorlineL, indicatorlineW)
H
hdx 已提交
83 84
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
85 86
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
      },
      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) {
100
          return
H
hdx 已提交
101
        }
102
        this.swiperIndex = index
H
hdx 已提交
103 104

        if (updateIndicator) {
105
          this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width)
H
hdx 已提交
106 107 108
        }
      },
      updateTabIndicator(left : Number, width : Number) {
109 110 111
        this.swiperIndicatorLineLeft = left
        this.swiperIndicatorLineWidth = width
        this.tabsScrollLeft = left + width / 2 - this.$swiperWidth / 2
H
hdx 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
      }
    }
  }
</script>

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

  .swiper-list {
    flex: 1;
  }

  .swiper-tabs {
    background-color: #ffffff;
  }

  .swiper-tabs-item {
    color: #555;
    font-size: 16px;
133
    padding: 12px 25px;
H
hdx 已提交
134 135
  }

136
  .swiper-tabs-item-active {
H
hdx 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    color: #007AFF;
  }

  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
  }

  .swiper-tabs-underline {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 0;
    background-color: #007AFF;
  }

  .swiper-view {
    flex: 1;
  }

  .swiper-item {
    flex: 1;
    align-items: center;
    justify-content: center;
  }

  .swiper-item-text {
    font-size: 72px;
    font-weight: bold;
  }
</style>