swiper-list.uvue 5.2 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">
4
      <!-- TODO 多一层 view -->
H
hdx 已提交
5 6
      <view>
        <view class="flex-row">
7 8
          <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)">
9 10
            {{item.title}}
          </text>
H
hdx 已提交
11
        </view>
12
        <view ref="indicator" class="swiper-tabs-indicator"></view>
H
hdx 已提交
13 14
      </view>
    </scroll-view>
15
    <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
16
      @animationfinish="onSwiperAnimationfinish">
H
hdx 已提交
17 18 19 20 21 22 23 24 25
      <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 已提交
26 27
    x : number,
    w : number
H
hdx 已提交
28 29 30
  }

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

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

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

H
hdx 已提交
73
        // 计算当前索引并重置差异
H
hdx 已提交
74 75 76 77
        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()

H
hdx 已提交
78
        // 计算目标索引及边界检查
H
hdx 已提交
79
        let move_to_index = current_index
80 81 82 83
        if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
          move_to_index += 1
        } else if (current_offset_x < 0 && move_to_index > 0) {
          move_to_index -= 1
H
hdx 已提交
84 85
        }

86
        // 计算偏移百分比
H
hdx 已提交
87 88
        const percentage = Math.abs(current_offset_x) / this.$swiperWidth

89 90
        // 通知更新指示线
        this.updateTabIndicator(current_index, move_to_index, percentage)
H
hdx 已提交
91 92
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
93 94
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
95
      },
H
hdx 已提交
96
      cacheTabItemsSize() {
H
hdx 已提交
97 98 99 100
        this.$swiperTabsRect.length = 0;
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
H
hdx 已提交
101 102
            x: node.offsetLeft,
            w: node.offsetWidth
H
hdx 已提交
103 104 105
          } as SwiperTabsItem)
        })
      },
H
hdx 已提交
106
      setSwiperIndex(index : number, updateIndicator : boolean) {
H
hdx 已提交
107
        if (this.swiperIndex === index) {
108
          return
H
hdx 已提交
109
        }
110

111
        this.swiperIndex = index
H
hdx 已提交
112 113

        if (updateIndicator) {
114
          this.updateTabIndicator(index, index, 1)
H
hdx 已提交
115 116
        }
      },
117 118 119 120
      updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
        // 计算指示线
        const current_size = this.$swiperTabsRect[current_index]
        const move_to_size = this.$swiperTabsRect[move_to_index]
H
hdx 已提交
121
        const indicator_line_x = current_size.x + (move_to_size.x - current_size.x) * percentage
122 123
        const indicator_line_w = current_size.w + (move_to_size.w - current_size.w) * percentage

124
        // 更新指示线
H
hdx 已提交
125
        const x = indicator_line_x + indicator_line_w / 2
126
        this.$indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
127

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

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

  .swiper-list {
    flex: 1;
  }

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

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

155
  .swiper-tabs-item-active {
H
hdx 已提交
156 157 158 159
    color: #007AFF;
  }

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