swiper-list.uvue 5.2 KB
Newer Older
H
hdx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
<template>
  <view class="swiper-list">
    <scroll-view class="swiper-tabs" :scroll-left="tabsScrollLeft" :scroll-x="true" :show-scrollbar="false">
      <view>
        <view class="flex-row">
          <view class="swiper-tabs-item" v-for="(item, index) in swiperList" :id="'swipertab' + index" ref="swipertab"
            :key="index" @click="onTabClick(index)">
            <text class="swiper-tabs-item-text"
              :class="swiperIndex==index ? 'uni-tab-item-title-active' : ''">{{item.title}}</text>
          </view>
        </view>
        <view class="swiper-tabs-indicator">
          <view class="swiper-tabs-underline"
            :style="{left: swiperIndicatorLineLeft + 'px', width: swiperIndicatorLineWidth + 'px'}"></view>
        </view>
      </view>
    </scroll-view>
    <swiper class="swiper-view" ref="swiper" :current="swiperIndex" :duration="300" @change="onSwiperChange"
      @transition="onSwiperTransition" @animationfinish="onSwiperAnimationfinish">
      <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 = {
    left : number,
    width : number
  }

  type SwiperViewItem = {
    title : string
  }

  export default {
    data() {
      return {
        swiperList: [] as SwiperViewItem[],
        swiperIndex: -1,
        tabsScrollLeft: 0,
        swiperIndicatorLineLeft: 0,
        swiperIndicatorLineWidth: 0,
        $lastSwiperIndex: 0,
        $swiperWidth: 0,
        $swiperTabsRect: [] as SwiperTabsItem[],
        $isTap: false
      }
    },
    onLoad() {
      for (let i = 0; i < 8; i++) {
        this.swiperList.push({
          title: "TAB " + i
        } as SwiperViewItem)
      }
    },
    onReady() {
      this.$swiperWidth = (this.$refs["swiper"]! as INode)!?.offsetWidth! as number;
      this.queryTabItemsSize();
      this.setSwiperIndex(0, true);
    },
    methods: {
      onTabClick(index : number) {
        this.setSwiperIndex(index, false);
      },
      onSwiperChange(e : SwiperChangeEvent) {
        this.setSwiperIndex(e.detail.current, false);
        console.log("onSwiperChange", e.detail.current);
      },
      onSwiperTransition(e : SwiperTransitionEvent) {
        const offsetX = e.detail.dx;

        let moveToIndex = offsetX > 0 ? this.$lastSwiperIndex + 1 : this.$lastSwiperIndex - 1
        if (moveToIndex < 0) { moveToIndex = 0 }
        if (moveToIndex > this.$swiperTabsRect.length - 1) { moveToIndex = this.$swiperTabsRect.length - 1 }

        const percentage = Math.abs(offsetX) / this.$swiperWidth;
        const currentSize = this.$swiperTabsRect[this.$lastSwiperIndex];
        const moveToSize = this.$swiperTabsRect[moveToIndex];
        const indicatorlineL = currentSize.left + (moveToSize.left - currentSize.left) * percentage;
        const indicatorlineW = currentSize.width + (moveToSize.width - currentSize.width) * percentage;

        this.updateTabIndicator(indicatorlineL, indicatorlineW);
        //console.log(this.$lastSwiperIndex, moveToIndex, offsetX, this.$swiperWidth, percentage);
      },
      // TODO 事件触发时机不对
      onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
        this.$lastSwiperIndex = e.detail.current;
        console.log("onSwiperAnimationfinish", e.detail.current);
        // this.setSwiperIndex(e.detail.current, true);
      },
      queryTabItemsSize() {
        this.$swiperTabsRect.length = 0;
        const tabs = this.$refs["swipertab"] as INode[]
        tabs.forEach((node) => {
          this.$swiperTabsRect.push({
            left: node.offsetLeft as number,
            width: node.offsetWidth as number
          } as SwiperTabsItem)
        })
      },
      setSwiperIndex(index : Number, updateIndicator : Boolean) {
        if (this.swiperIndex === index) {
          return;
        }
        this.swiperIndex = index;

        if (updateIndicator) {
          this.updateTabIndicator(this.$swiperTabsRect[index].left, this.$swiperTabsRect[index].width);
        }
      },
      updateTabIndicator(left : Number, width : Number) {
        this.swiperIndicatorLineLeft = left;
        this.swiperIndicatorLineWidth = width;
        const offset = left + width / 2;
        if (offset > this.$swiperWidth / 2) {
          this.tabsScrollLeft = offset - this.$swiperWidth / 2;
        }
      }
    }
  }
</script>

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

  .swiper-list {
    flex: 1;
  }

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

  .swiper-tabs-item {
    padding: 12px 25px;
  }

  .swiper-tabs-item-text {
    color: #555;
    font-size: 16px;
  }

  .swiper-tabs-item-text-active {
    color: #007AFF;
  }

  .swiper-tabs-indicator {
    position: relative;
    height: 2px;
  }

  .swiper-tabs-underline {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 0;
    background-color: #007AFF;
  }

  .swiper-tabs-animation {
    transition-duration: 0.2s;
    transition-property: left;
  }

  .swiper-view {
    flex: 1;
  }

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

  .swiper-item-text {
    font-size: 72px;
    font-weight: bold;
    margin: auto;
  }
</style>