短視訊平臺搭建,淡入淡出 支援左滑右滑輪播圖

zhibo系統開發發表於2022-06-29

短視訊平臺搭建,淡入淡出 支援左滑右滑輪播圖

<template>
  <view>
    <view @touchstart="touchStart" @touchend="touchEnd">
      <image v-for="(value,index) in swiperList" :key="index" :src="value.imagePath" :animation="num==index?showpic:hidepic"/>
      <view>
        <view v-for="(item,ind) in swiperList" :key="ind" :class="num==ind?'activeItem':''"></view>
      </view>
    </view>
  </view>
</template>
<script>
 
export default {
  name: 'grapSwiper',
  data () {
    return {
      touchStartX: 0,  // 觸屏起始點x  
      touchStartY: 0,  // 觸屏起始點y  
      swiperList:[
        {
          imagePath:'https://pla.o-banks.cn/attach/mgrImg/20220110/1641806731686.png'
        },
        {
         imagePath:'
        },
        {
          imagePath:'
        }
      ],
      setTime:null,
      num:0,
      timeCount:5000,
      showpic:null,
      hidepic:null
    }
  },
  created () {
  },
  mounted(){
    this.initSwiper();
  },
  methods:{
    initSwiper(){
      const animation = uni.createAnimation({}) //建立一個動畫例項
      this.setTime = setInterval(() => {
        this.num++
        if(this.num >this.swiperList.length-1){
          this.num = 0
        }
        //淡入
        animation.opacity(1).step({duration:300}) //描述動畫
        this.showpic = animation.export()
        //淡出
        animation.opacity(0).step({duration:300})
        this.hidepic=animation.export()
 
      }, this.timeCount);
    },
    touchStart(e) {  
      this.touchStartX = e.touches[0].clientX;  
      this.touchStartY = e.touches[0].clientY;  
    },  
    touchEnd(e) {  
      let deltaX = e.changedTouches[0].clientX - this.touchStartX;  
      let deltaY = e.changedTouches[0].clientY - this.touchStartY;  
      if (Math.abs(deltaX) > 50 && Math.abs(deltaX) > Math.abs(deltaY)) {  
        // 左滑右滑時重新初始化定時器
        clearInterval(this.setTime)
        this.initSwiper();
 
        if (deltaX >= 0) {  
          this.num--
          if(this.num <0){
            this.num = this.swiperList.length-1
          }
        } else {  
          this.num++
          if(this.num >this.swiperList.length-1){
            this.num = 0
          }
        }  
      } else {  
        console.log("可能是誤觸!")  
      }  
    }, 
  },
  destroyed(){
    clearInterval(this.setTime)
  }
}
</script>
 
<style scoped>
  .grapSwiper{
    .banner{
      width: 750rpx;
      height: 640rpx;
      position: relative;
      .pic{
        width: 750rpx;
        height: 640rpx;
        position: absolute;
      }
      .snipper{
        position: absolute;
        width: 100%;
        height: 2rpx;
        top: 210rpx;
        left: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        .sniItem{
          width: 15rpx;
          height: 2rpx;
          background: #FFFFFF;
          opacity: 0.4;
          border-radius: 5rpx;
          margin-right: 6rpx;
        }
        .activeItem{
          width: 25rpx;
          background: #FFFFFF;
          opacity: 1;
        }
      }
    }
  }
</style>

以上就是短視訊平臺搭建,淡入淡出 支援左滑右滑輪播圖, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2903337/,如需轉載,請註明出處,否則將追究法律責任。

相關文章