SwiperComponent.vue 8.43 KB
<template>
  <view class="swiper-box">
    <movable-area
      class="swiper-carousel"
      @touchstart="starts"
      @touchend="ends"
    >
      <template v-for="(item,index) in imagelist">
        <movable-view
          :key="index"
          :class="['carousel-block',item.class]"
          @click.stop="changeBlock(item)"
          direction="horizontal"
          @change="change"
          :disabled="item.class != 'block-center'"
          :x="item.class != 'block-center' ? 0 : viewX"
          :data-index="item.isIndex"
        >
          <image
            class="image-block"
            :src="item.imgUrl"
            @load="item.hide = true"
          />
          <view :class="['error-block',{hide:item.hide}]">
            <uni-icons
              :type="icon"
              :size="size"
              color="#909399"
            ></uni-icons>
          </view>
        </movable-view>
      </template>
    </movable-area>
    <view class="module-title flex">
      {{moduleTitle}}
    </view>
    <view class="module-dots flex">
      <template v-for="(i,index) in moduleList">
        <div
          :class="['dot', {'active': index == currentIndex}]"
          :key="index"
        >
        </div>
      </template>
    </view>
  </view>
</template>
<script>
import utils from "@/utils/utils.js";

export default {
  name: 'SwiperComponent',
  props: {
    moduleList: {
      type: Array,
      default: () => []
    },
    imgKey: {
      type: String,
      default: 'thumbnail'
    },
    icon: {
      type: String,
      default: 'image'
    },
    size: {
      type: [Number, String],
      default: '40'
    },
    interval: {
      type: [Number, String],
      default: 3000
    },
    autoplay: {
      type: Boolean,
      default: true
    },
    stopAutoPlay: {
      type: Boolean,
      default: false
    },
  },
  watch: {
    moduleList: {
      handler (val) {
        if (val.length && !this.imagelist.length) this.getDatalist()
      },
      deep: true
    },
    stopAutoPlay: {
      handler (val) {
        if (this.autoplay && !val) this.autoPlayFunction()
      },
      deep: true
    }
  },
  computed: {
    moduleTitle () {
      return this.moduleList[this.currentIndex]?.detailTitle
    }
  },
  created () {
    // this.getDatalist()
  },
  data () {
    return {
      datalist: [],
      imagelist: [],
      ismove: 0,
      currentIndex: 0,
      autoPlayTimer: null,
      endTimer: null,
      playStatus: true,
      style_object: Object.freeze([
        { index: 0, class: 'block-left' },
        { index: 1, class: 'block-center' },
        { index: 2, class: 'block-right' }
      ]),
      blockX: 0,
      viewX: 0
    }
  },
  beforeDestroy () {
    // 组件销毁时清理自动播放定时器
    if (this.autoPlayTimer) {
      clearTimeout(this.autoPlayTimer);
      this.autoPlayTimer = null;
    }
  },
  methods: {
    getDatalist () {
      if (this.moduleList) {
        this.datalist = JSON.parse(JSON.stringify(this.moduleList))
        let num = 3
        let diff = num - this.moduleList.length
        if (diff > 0) {
          for (let i = 0; i < diff; i++) {
            let obj = {}
            obj[this.imgKey] = ''
            this.datalist.push(obj)
          }
        }
        this.getImagelist()
      }
    },
    getImagelist () {
      let { datalist, autoplay } = this
      this.imagelist = JSON.parse(JSON.stringify(this.style_object))
      this.imagelist.forEach((item, index) => {
        const dataIndex = index - 1 < 0 ? datalist.length - 1 : index - 1
        item.isIndex = dataIndex
        item.isToor = index
        item.imgUrl = datalist[dataIndex]?.[this.imgKey] || ''
        item.x = 0
      })
      if (autoplay) this.autoPlayFunction()
    },
    autoPlayFunction () {
      if (this.stopAutoPlay || !this.playStatus) {
        if (this.autoPlayTimer) {
          clearTimeout(this.autoPlayTimer);
          this.autoPlayTimer = null;
        }
        return;
      }
      // 设置新的自动播放定时器
      this.autoPlayTimer = setTimeout(() => {
        // 再次检查是否需要停止播放
        if (this.stopAutoPlay || !this.playStatus) {
          clearTimeout(this.autoPlayTimer);
          this.autoPlayTimer = null;
          return;
        }

        // 执行轮播动画并递归调用自身
        this.updateCarouselPositions();
        this.autoPlayFunction();
      }, this.interval);
    },
    updateCarouselPositions (direction = 'left') {
      let { imagelist, datalist } = this,
        len = datalist.length - 1,
        list = JSON.parse(JSON.stringify(this.style_object))
      this.imagelist.forEach(item => {
        let toor = direction == 'left' ?
          (item.isToor - 1) < 0 ? imagelist.length - 1 : item.isToor - 1
          : (item.isToor + 1) > imagelist.length - 1 ? 0 : item.isToor + 1
        item.isToor = toor
        item.imgUrl = ''
        item.class = list[toor].class
        item.x = 0
        let isIndex = item.isIndex

        if (direction == 'left' && item.class == 'block-right') {
          if (isIndex == len) isIndex = 2
          else if (isIndex == len - 1) isIndex = 1
          else isIndex += 3
          if (isIndex > len) isIndex = 0
        }
        if (direction == 'right' && item.class == 'block-left') {
          if (isIndex == 1) isIndex = len - 1
          else if (isIndex == 0) isIndex = len - 2
          else isIndex -= 3
          if (isIndex < 0) isIndex = len
        }
        item.isIndex = isIndex
        item.imgUrl = datalist[isIndex][this.imgKey] || ''
        item.index = list[toor].index
      })
      let currentIndex = this.imagelist.findIndex(item => item.index == 1)
      this.currentIndex = this.imagelist[currentIndex].isIndex
    },
    changeBlock (item) {
      let toor = item.isToor
      if (toor === 0) this.updateCarouselPositions('right')
      if (toor == 2) this.updateCarouselPositions()
      this.$emit('click', item.isIndex)
    },
    starts (e) {
      this.ismove = e.touches[0].pageX
      this.playStatus = false
      if (this.endTimer) {
        clearTimeout(this.endTimer);
        this.endTimer = null;
        return;
      }
    },
    ends (e) {
      let { blockX, ismove } = this
      let pageX = e.changedTouches[0].pageX
      let nums = ismove - pageX
      if (!(nums > 50 || blockX < -30) || !(nums < -50 || blockX > 30)) {
        this.viewX = -10
        setTimeout(() => {
          this.viewX = 0
        }, 16)
      }
      if (nums > 50 || blockX < -30) this.updateCarouselPositions()
      if (nums < -50 || blockX > 30) this.updateCarouselPositions('right')


      this.endTimer = setTimeout(() => {
        if (!this.playStatus) {
          this.playStatus = true
          this.autoPlayFunction()
        }
      }, this.interval)
    },
    change: utils.throttle(function (e, item) {
      let { x } = e.detail
      this.blockX = x
    }, 16)
  }
}
</script>
<style lang="scss" scoped>
.swiper-carousel {
  width: 100%;
  height: toRpx(440);
  position: relative;
  overflow: hidden;
  .carousel-block {
    position: absolute;
    border-radius: 10upx;
    overflow: hidden;
    box-shadow: 0 0 5px #0000001a;
    transition: all 0.2s ease; /* 添加过渡 */
    border: toRpx(2) solid #ffffffb3;
    box-shadow: 0 toRpx(3) toRpx(12) 0 #5294ff33;
    border-radius: toRpx(24);
    .image-block {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-size: 100% 100%;
      z-index: 5;
    }
    .error-block {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: #f5f7fa;
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 1;
    }

    .hide {
      opacity: 0;
    }
  }
  .block-left,
  .block-right {
    top: toRpx(68);
    height: toRpx(306);
    width: toRpx(468);

    transition: all 0.1s;
  }
  .block-left {
    left: toRpx(150);
    z-index: 10;
  }
  .block-right {
    left: toRpx(592);
    z-index: 15;
  }
  .block-center {
    left: toRpx(294);
    top: toRpx(16);
    height: toRpx(408);
    width: toRpx(624);
    z-index: 20;
    transition: all 0.3s;
  }
}
.module-title {
  color: #333333;
  font-size: toRpx(24);
  margin-top: toRpx(24);
  justify-content: center;
  position: relative;
}
.module-dots {
  position: absolute;
  bottom: 56rpx;
  right: toRpx(48);
  gap: toRpx(12);
  .dot {
    width: toRpx(12);
    height: toRpx(12);
    opacity: 1;
    background: #0007400d;
    border-radius: 100%;
  }
  .active {
    background: #fff;
  }
}
</style>