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

  type SwiperViewItem = {
28
    title : string,
H
hdx 已提交
29 30
  }

H
hdx 已提交
31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * 根据权重在两个值之间执行线性插值.
   * @constructor
   * @param {number} value1 - 第一个值,该值应为下限.
   * @param {number} value2 - 第二个值,该值应为上限.
   * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
   * @returns {number} 内插值
   */
  function lerpNumber(value1 : number, value2 : number, amount : number) : number {
    return (value1 + (value2 - value1) * amount)
  }

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

H
hdx 已提交
80 81
        const offset_x = e.detail.dx

H
hdx 已提交
82
        // 计算当前索引并重置差异
H
hdx 已提交
83 84 85 86
        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 已提交
87
        // 计算目标索引及边界检查
H
hdx 已提交
88
        let move_to_index = current_index
89 90 91 92
        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 已提交
93 94
        }

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

98 99
        // 通知更新指示线
        this.updateTabIndicator(current_index, move_to_index, percentage)
H
hdx 已提交
100 101
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
102 103
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
104
      },
H
hdx 已提交
105
      cacheTabItemsSize() {
H
hdx 已提交
106 107 108 109
        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) {
123
          this.updateTabIndicator(index, index, 1)
H
hdx 已提交
124 125
        }
      },
126 127 128 129
      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 已提交
130 131
        const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
        const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
132

133
        // 更新指示线
H
hdx 已提交
134
        const x = indicator_line_x + indicator_line_w / 2
H
hdx 已提交
135
        this.$indicatorNode!.style.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
136

H
hdx 已提交
137
        // 滚动到水平中心位置
138 139
        const scroll_x = x - this.$swiperWidth / 2
        this.$tabScrollView?.setAttribute('scrollLeft', scroll_x)
H
hdx 已提交
140 141 142 143 144 145 146 147
      }
    }
  }
</script>

<style>
  .flex-row {
    flex-direction: row;
148
    align-self: flex-start;
H
hdx 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161
  }

  .swiper-list {
    flex: 1;
  }

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

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

165
  .swiper-tabs-item-active {
H
hdx 已提交
166 167 168 169
    color: #007AFF;
  }

  .swiper-tabs-indicator {
170
    width: 1px;
H
hdx 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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;
  }
189
</style>