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
      <!-- 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
  }

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

H
hdx 已提交
83 84
        const offset_x = e.detail.dx

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

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

101 102
        // 通知更新指示线
        this.updateTabIndicator(current_index, move_to_index, percentage)
H
hdx 已提交
103 104
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
105 106
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
107
      },
H
hdx 已提交
108
      cacheTabItemsSize() {
H
hdx 已提交
109 110 111 112
        this.$swiperTabsRect.length = 0;
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
H
hdx 已提交
113 114
            x: node.offsetLeft,
            w: node.offsetWidth
H
hdx 已提交
115 116 117
          } as SwiperTabsItem)
        })
      },
H
hdx 已提交
118
      setSwiperIndex(index : number, updateIndicator : boolean) {
H
hdx 已提交
119
        if (this.swiperIndex === index) {
120
          return
H
hdx 已提交
121
        }
122

123
        this.swiperIndex = index
H
hdx 已提交
124 125

        if (updateIndicator) {
126
          this.updateTabIndicator(index, index, 1)
H
hdx 已提交
127 128
        }
      },
129 130 131 132
      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 已提交
133 134
        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)
135

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

H
hdx 已提交
140
        // 滚动到水平中心位置
141 142
        const scroll_x = x - this.$swiperWidth / 2
        this.$tabScrollView?.setAttribute('scrollLeft', scroll_x)
H
hdx 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      }
    }
  }
</script>

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

  .swiper-list {
    flex: 1;
  }

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

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

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

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