swiper-list.uvue 5.3 KB
Newer Older
H
hdx 已提交
1 2
<template>
  <view class="swiper-list">
3
    <scroll-view ref="tabScroll" class="swiper-tabs" :scroll-x="true" :show-scrollbar="false">
H
hdx 已提交
4 5
      <view>
        <view class="flex-row">
6 7
          <text ref="swipertab" class="swiper-tabs-item" :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''"
            v-for="(item, index) in swiperList" :key="index" @click="onTabClick(index)">
8 9
            {{item.title}}
          </text>
H
hdx 已提交
10
        </view>
11
        <view ref="indicator" class="swiper-tabs-indicator"></view>
H
hdx 已提交
12 13
      </view>
    </scroll-view>
14
    <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
15
      @animationfinish="onSwiperAnimationfinish">
H
hdx 已提交
16 17 18 19 20 21 22 23 24
      <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 = {
H
hdx 已提交
25 26
    x : number,
    w : number
H
hdx 已提交
27 28 29
  }

  type SwiperViewItem = {
30
    title : string,
H
hdx 已提交
31 32 33 34 35 36 37
  }

  export default {
    data() {
      return {
        swiperList: [] as SwiperViewItem[],
        swiperIndex: -1,
38 39
        $tabScrollView: null as null | INode,
        $indicatorNode: null as null | INode,
40
        $animationFinishIndex: 0,
H
hdx 已提交
41
        $swiperWidth: 0,
42
        $swiperTabsRect: [] as SwiperTabsItem[]
H
hdx 已提交
43 44 45 46
      }
    },
    onLoad() {
      for (let i = 0; i < 8; i++) {
47
        const space = " ".repeat(i)
H
hdx 已提交
48
        this.swiperList.push({
49
          title: "Tab " + space + i
H
hdx 已提交
50 51 52 53
        } as SwiperViewItem)
      }
    },
    onReady() {
54 55
      this.$tabScrollView = this.$refs.get('tabScroll') as INode
      this.$indicatorNode = this.$refs.get('indicator') as INode
H
hdx 已提交
56
      this.$swiperWidth = (this.$refs["swiper"] as INode).getBoundingClientRect().width
57 58
      this.queryTabItemsSize()
      this.setSwiperIndex(0, true)
H
hdx 已提交
59 60 61
    },
    methods: {
      onTabClick(index : number) {
62
        this.setSwiperIndex(index, false)
H
hdx 已提交
63 64
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
65 66 67 68 69
        // skyline 每项完成触发 Animationfinish,偏移值重置
        // webview 全部完成触发 Animationfinish,偏移值累加
        // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
        // uni-app-x 和 webview 行为一致

H
hdx 已提交
70 71
        const offset_x = e.detail.dx

72
        // 重置差异,计算当前索引
H
hdx 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        const current_offset_x = offset_x % this.$swiperWidth
        const current_offset_i = offset_x / this.$swiperWidth
        const current_index = this.$animationFinishIndex + current_offset_i.toInt()

        // 边界检查
        let move_to_index = current_index
        if (current_offset_x > 0) {
          if (move_to_index < this.$swiperTabsRect.length - 1) {
            move_to_index += 1
          }
        } else if (current_offset_x < 0) {
          if (move_to_index > 0) {
            move_to_index -= 1
          }
        }

89
        // 计算指示线位置和大小
H
hdx 已提交
90 91 92 93 94 95
        const percentage = Math.abs(current_offset_x) / this.$swiperWidth
        const current_size = this.$swiperTabsRect[current_index]
        const move_to_size = this.$swiperTabsRect[move_to_index]
        const indicator_line_l = current_size.x + (move_to_size.x - current_size.x) * percentage
        const indicator_line_w = current_size.w + (move_to_size.w - current_size.w) * percentage

96
        // 更新指示线位置和大小
H
hdx 已提交
97 98 99
        this.updateTabIndicator(indicator_line_l, indicator_line_w)

        // console.log(current_index + "->" + move_to_index + " x=" + offset_x + " p=" + percentage)
H
hdx 已提交
100 101
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
102 103
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
104 105 106 107 108 109
      },
      queryTabItemsSize() {
        this.$swiperTabsRect.length = 0;
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
H
hdx 已提交
110 111
            x: node.offsetLeft,
            w: node.offsetWidth
H
hdx 已提交
112 113 114
          } as SwiperTabsItem)
        })
      },
H
hdx 已提交
115
      setSwiperIndex(index : number, updateIndicator : boolean) {
H
hdx 已提交
116
        if (this.swiperIndex === index) {
117
          return
H
hdx 已提交
118
        }
119

120
        this.swiperIndex = index
H
hdx 已提交
121 122

        if (updateIndicator) {
H
hdx 已提交
123
          this.updateTabIndicator(this.$swiperTabsRect[index].x, this.$swiperTabsRect[index].w)
H
hdx 已提交
124 125
        }
      },
H
hdx 已提交
126
      updateTabIndicator(left : number, width : number) {
127 128 129 130 131 132 133
        // 更新指示线
        const x = left + width / 2
        this.$indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${width}px)`)

        // 将指示线滚动到水平中心位置
        const scroll_x = x - this.$swiperWidth / 2
        this.$tabScrollView?.setAttribute('scrollLeft', scroll_x)
H
hdx 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      }
    }
  }
</script>

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

  .swiper-list {
    flex: 1;
  }

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

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

158
  .swiper-tabs-item-active {
H
hdx 已提交
159 160 161 162
    color: #007AFF;
  }

  .swiper-tabs-indicator {
163
    width: 1px;
H
hdx 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    height: 2px;
    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>