随笔 - 36, 文章 - 0, 评论 - 0, 阅读 - 15403
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on   学以致用的人  阅读(1675)  评论(0编辑  收藏  举报
复制代码
   -----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组件的灵活使用;

复制代码
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示