swiper-list2.uvue 5.2 KB
Newer Older
1 2
<template>
  <view class="swiper-list">
雪洛's avatar
雪洛 已提交
3
    <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
4 5 6 7 8 9 10 11 12 13 14
      <view class="flex-row" style="align-self: flex-start;">
        <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>
      </view>
    </scroll-view>
    <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
      @animationfinish="onSwiperAnimationfinish">
      <swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
        <text class="swiper-item-text">{{index}}</text>
W
wanganxp 已提交
15
        <!-- 可以将上述text组件替换为list组件,实现可左右滑动的长列表 -->
16 17 18 19 20 21 22 23 24 25
      </swiper-item>
    </swiper>
  </view>
</template>

<script>
  type SwiperViewItem = {
    title : string,
  }

H
hdx 已提交
26 27 28 29 30 31 32 33 34 35 36 37
  /**
   * 根据权重在两个值之间执行线性插值.
   * @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)
  }

38 39 40 41
  export default {
    data() {
      return {
        swiperList: [] as SwiperViewItem[],
42 43 44 45
        swiperIndex: 0,
        tabScrollView: null as null | UniElement,
        animationFinishIndex: 0,
        swiperWidth: 0
46 47 48 49 50 51 52 53 54 55
      }
    },
    onLoad() {
      for (let i = 0; i < 4; i++) {
        this.swiperList.push({
          title: "Tab " + i
        } as SwiperViewItem)
      }
    },
    onReady() {
H
hdx 已提交
56 57 58 59 60
      this.tabScrollView = this.$refs['tabScroll'] as UniElement;
      (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync().then((res : DOMRect) => {
        this.swiperWidth = res.width
        this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
      });
61 62 63 64 65 66 67 68 69
    },
    methods: {
      onTabClick(index : number) {
        this.setSwiperIndex(index, false)
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
        const offset_x = e.detail.dx

        // 计算当前索引并重置差异
70 71 72
        const current_offset_x = offset_x % this.swiperWidth
        const current_offset_i = offset_x / this.swiperWidth
        const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
73 74 75 76 77 78 79 80 81 82

        // 计算目标索引及边界检查
        let move_to_index = current_index
        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
        }

        // 计算偏移百分比
83
        const percentage = Math.abs(current_offset_x) / this.swiperWidth
84 85

        // 通知更新指示线
86 87 88
        if (current_index != move_to_index) {
          this.updateTabIndicator(current_index, move_to_index, percentage)
        }
89 90 91
      },
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        this.setSwiperIndex(e.detail.current, true)
92
        this.animationFinishIndex = e.detail.current
93 94 95 96 97 98 99 100 101 102 103 104 105
      },
      setSwiperIndex(index : number, updateIndicator : boolean) {
        if (this.swiperIndex == index) {
          return
        }

        this.swiperIndex = index

        if (updateIndicator) {
          this.updateTabIndicator(index, index, 1)
        }
      },
      updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
106
        if (percentage == 0) {
107 108 109 110 111
          return
        }

        // 缩放范围
        const min_ratio = 1
112
        const max_ratio = 1.3
113

114
        const tabs = this.$refs['swipertab'] as UniElement[]
115 116
        const current_node = tabs[current_index]
        const move_to_node = tabs[move_to_index]
117 118

        // 当前
H
hdx 已提交
119
        const current_scale = lerpNumber(min_ratio, max_ratio, 1 - percentage)
H
hdx 已提交
120
        current_node.style.setProperty('transform', `scale(${current_scale})`)
121 122

        // 目标
H
hdx 已提交
123
        const move_to_scale = lerpNumber(min_ratio, max_ratio, percentage)
H
hdx 已提交
124
        move_to_node.style.setProperty('transform', `scale(${move_to_scale})`)
125 126

        // 滚动到水平中心位置
H
hdx 已提交
127
        const target_x = lerpNumber(current_node.offsetLeft, move_to_node.offsetLeft, percentage)
128
        const center_x = target_x + move_to_node.offsetWidth / 2 - this.swiperWidth / 2
H
hdx 已提交
129 130
        // app 平台后续支持 scrollTo()
        // #ifndef MP-WEIXIN
131
        this.tabScrollView!.scrollLeft = center_x
H
hdx 已提交
132 133 134 135 136 137
        // #endif
        // #ifdef MP-WEIXIN
        this.tabScrollView!.scrollTo({
          left: center_x
        })
        // #endif
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-item {
    color: #555;
    font-size: 16px;
H
hdx 已提交
155
    margin: 15px 25px 5px 25px;
156
    white-space: nowrap;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  }

  .swiper-tabs-item-active {
    color: #000000;
  }

  .swiper-view {
    flex: 1;
  }

  .swiper-item {
    flex: 1;
    align-items: center;
    justify-content: center;
  }

  .swiper-item-text {
    font-size: 72px;
    font-weight: bold;
  }
H
hdx 已提交
177
</style>