uniapp带视频和图片的轮播swiper
<view class="carousel">
<swiper @change = "swiperChange" indicator-dots circular="true" duration="400" autoplay="true">
<swiper-item v-if="goodsData.picVideo"> <!-- @click="showVideo" :style="showFullVideo==true?'display:none':'display:block'" class="swiper-item" -->
<!-- MP-WEIXIN -->
<!-- #ifdef MP-WEIXIN -->
<video id="firstVideo" @pause="videoPause" v-if="goodsData.picVideo&&showFullVideo==true" direction=0 style="width: 100%;height: 722upx;z-index:9999" mode="widthFix" :src="goodsData.picVideo"></video>
<!-- #endif -->
<!-- #ifdef H5 -->
<video id="firstVideo" :enable-progress-gesture="false" :poster="goodsData.picVideo+'?vframe/jpg/offset/1'" :autoplay="true" @pause="videoPause" v-if="goodsData.picVideo&&showFullVideo==true" direction=0 style="width: 100%;height: 722upx;z-index:9999" mode="widthFix" :src="goodsData.picVideo"></video>
<!-- #endif -->
</swiper-item>
<swiper-item class="swiper-item" v-for="(item,index) in imgList" :key="index">
<view class="image-wrapper">
<image v-if="item.src.indexOf('.mp4')==-1" :src="item.src" class="loaded" mode="aspectFill"></image>
<!-- <video v-if="goodsData.picVideo" style="width: 18.8rem;height: 18.8rem;" autoplay="true" loop="false" muted="true" mode="widthFix" :src="goodsData.picVideo"></video> -->
</view>
</swiper-item>
</swiper>
</view>
data添加参数showFullVideo:true
onShow()添加获取视频环境方法
onShow() {
this.player = uni.createVideoContext("firstVideo")
},
methods添加方法swiperChange
swiperChange(current, source){
// 切换如果不是视频位置 暂停视频
console.log(current.target.current)
if(current.target.current == 0){
//this.player.seek(0)
this.player.play()
//this.showFullVideo=true;
}else{
this.player.pause()
//this.player.stop()
//this.showFullVideo=false;
}
},
完成!
遇到的问题:
做的中间一度swiper-item挡住了video控制条点不了不知道为什么,第二天再试的时候又好了,按理说video应该属于最高层级,非常奇怪有待研究。
另外如果使用this.player.play()
和this.player.pause()
,微信小程序页面的时候最好使用this.player = uni.createVideoContext('firstVideo',this)
方式。
还有,video的滑动效果会导致swiper的滑动效果不顺畅出现卡顿,查询资料发现是手动滑动轮播时触发到了video标签的事件,需要在video上加上:enable-progress-gesture="false"
,禁止video的滑动效果。