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">
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
58 59
      this.queryTabItemsSize()
      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

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()

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
        // console.log(current_index + "->" + move_to_index+ " p=" + percentage + " x=" + offset_x )
H
hdx 已提交
93 94
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
95 96
        this.setSwiperIndex(e.detail.current, true)
        this.$animationFinishIndex = e.detail.current
H
hdx 已提交
97 98 99 100 101 102
      },
      queryTabItemsSize() {
        this.$swiperTabsRect.length = 0;
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
H
hdx 已提交
103 104
            x: node.offsetLeft,
            w: node.offsetWidth
H
hdx 已提交
105 106 107
          } as SwiperTabsItem)
        })
      },
H
hdx 已提交
108
      setSwiperIndex(index : number, updateIndicator : boolean) {
H
hdx 已提交
109
        if (this.swiperIndex === index) {
110
          return
H
hdx 已提交
111
        }
112

113
        this.swiperIndex = index
H
hdx 已提交
114 115

        if (updateIndicator) {
116
          this.updateTabIndicator(index, index, 1)
H
hdx 已提交
117 118
        }
      },
119 120 121 122 123 124 125
      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]
        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

126
        // 更新指示线
127 128
        const x = indicator_line_l + indicator_line_w / 2
        this.$indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
129 130 131 132

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

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

  .swiper-list {
    flex: 1;
  }

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

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

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

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