vue H5 超简单的swiper制作抖音上拉切换视频播放


   -----html部分------
<swiper vertical :style="{height: windowheight+'px',width:375+'px'}"  class="swiper-top" @change="playNow">
  <swiper-item v-for="(item1,index1) in ['1','2','3']" :key ="index1">
    <view :style="{height: windowheight+'px',width:375+'px'}" @click="controlVideo">
          <template >
              <video :id="`postDetail${index1}`" :ref="`postDetail${index1}`"  :enable-play-gesture="true" :src="postDetail.video_url"  :show-fullscreen-btn="false"  object-fit="contain" play-btn-position="center"  :style="{height: windowheight+'px',width:375+'px'}"></video >
          </template>
    </view>
  </swiper-item>
</swiper>    

-------------

<script>
export default {

  data() {
    return {

      videoPlay:true, 

      current:0,//滑块第几个
      videoContext:undefined //全局定义视频API变量

      }

  },

      this.playNow(1);//把这个放到初始化页面的函数内,即可进入页面自动播放,不放就不会自动播放

  methods: {

   async playNow(e){
      if(e.detail!=undefined){
      this.current=e.detail.current;
      this.videoContext.pause()
      this.videoContext.seek(0)
      this.$off(this.videoContext);
       }
      this.videoContext = await uni.createVideoContext(`postDetail${this.current}`)
      this.videoContext.play();

      this.videoPlay=true;
    },

    controlVideo(){ //多写的一个单击蒙版控制播放暂停,也可以写在playNow内,传不同的值来控制就行
      if(this.videoPlay){
        this.videoContext.pause() //暂停
        this.videoContext.seek(0) //复原播放时间到0秒
        this.videoPlay=false;
      }else{
        this.videoContext.play() //播放
        this.videoPlay=true;
      }
    },

 

 

  }

}

</script>

    主要是针对uniapp的video组件的灵活使用;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM