-----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组件的灵活使用;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~