swiper和video实现抖音刷视频功能

实现抖音刷视频功能 

<!--swiper实现整屏划动播放视频-->
<swiper :vertical="true" class="swiperVideo" style="height: 100vh;" @change="changeplay"
			@touchstart="touchStart" @touchend="touchEnd">
	<swiper-item v-for="(item, index) in swiperList" :key="item.id">
		<view class="bg-video">
			<view class="video">
				<videoPlayer ref="player" :video="item" :index="index"></videoPlayer>
			</view>
		</view>
	</swiper-item>
</swiper>

//上下滑动触发事件
changeplay(res) {
	console.log('res', res);
	clearTimeout(time)
	this.page = res.detail.current
	time = setTimeout(() => {
		if (this.pageStatrY < this.pageEndY) {

			console.log('向上滑动')
			setTimeout(() => {
				this.$refs.player[this.page].player()
			}, 20)

			this.$refs.player[this.page + 1].pause()
			this.pageStatrY = 0
			this.pageEndY = 0
		} else {
			console.log('向下滑动')
			setTimeout(() => {
				this.$refs.player[this.page].player()
			}, 20)
			console.log('page', this.page - 1, this.$refs.player);
			this.$refs.player[this.page - 1].pause()
			this.pageStatrY = 0
			this.pageEndY = 0
		}
	}, 1)
},
//获取向下滑动的值
touchStart(res) {

	this.pageStatrY = res.changedTouches[0].pageY
	console.log('pageStatrY', this.pageStatrY)
},
//获取向上滑动的值
touchEnd(res) {
	this.pageEndY = res.changedTouches[0].pageY
	console.log('pageEndY', this.pageEndY)
},

videoPlayer 组件

<template>
	<view class="videoPlayer">
		<video id="myVideo" class="video" :controls="false" :src="video" :loop="false" :autoplay="autoplay"
			:show-center-play-btn="false" style="pointer-events:none;will-change: transform;" @click="click"></video>
		
	</view>
</template>

<script>
	var timer = null
	export default {
		props: ['video', 'index'],
		data() { 
			return {
				play: false,
				dblClick: false,
				autoplay: false,
			};
		},
		mounted() {  
			this.videoContext = uni.createVideoContext('myVideo', this)
			this.atuo() 
		},

		methods: {
			click() {
				clearTimeout(timer)
				this.dblClick = !this.dblClick
				timer = setTimeout(() => {
					if (this.dblClick) { //判断是单击 即为true
						//单击
						if (this.play === false) {
							this.playThis()
						} else {
							this.pause()
						}
					} else {
						//双击
						// this.$emit('changeClick') //向父组件传递一个事件
					}
					this.dblClick = false //点击后重置状态 重置为false
				}, 300)
			},
			player() {
				//从头播放视频
				if (this.play === false) {
					this.videoContext.seek(0)
					this.videoContext.play()
					this.play = true
				}
			},
			pause() {
				//暂停视频
				if (this.play === true) {
					this.videoContext.pause()
					this.play = false
				}
			},
			playThis() {
				//播放当前视频
				if (this.play === false) {
					this.videoContext.play()
					this.play = true
				}
			},
			//首个视频自动播放
			atuo() {
				//首个视频自动播放
				if (this.index === 0) {
					this.autoplay = true
				}
			}
		},
	}
</script>

<style>
	.videoPlayer {
		height: 100vh;
		width: 100%;
	}

	.video {
		height: 100vh;
		width: 100%;
	}
</style>

!!!!目前我这个小程序是可以用的,但是华为快应用不行(uniapp转华为快应用)

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现类似抖音的滑动视频功能,你可以按照以下步骤进行操作: 1. 使用 Swiper 组件:在微信小程序中,可以使用 Swiper 组件来实现滑动功能。将每个 swiper-item 视作一个视频项,通过设置 Swiper 组件的属性和事件来实现滑动效果。 2. 加载视频数据:从后台获取视频列表数据,并将每个视频的信息(比如视频地址、封面图等)保存在一个数组中。 3. 渲染视频列表:使用 wx:for 指令遍历视频数组,并在 swiper-item 中渲染每个视频的封面图。 4. 播放视频:当用户滑动到某个 swiper-item 时,触发 Swiper 组件的 change 事件。在事件处理函数中,获取当前 swiper-item 的索引,然后根据索引从视频数组中获取对应的视频地址,使用 video 组件进行播放。 5. 自动播放下一个视频:当当前视频播放完毕时,可以通过 video 组件的 ended 事件监听,触发自动播放下一个视频的逻辑。在事件处理函数中,根据当前 swiper-item 的索引,切换到下一个 swiper-item。 6. 视频切换效果:可以通过设置 Swiper 组件的 animation 参数,来实现视频切换时的过渡效果。 7. 其他功能:根据你的需求,还可以添加点赞、评论、分享等功能,通过添加相应的按钮和交互逻辑实现。 以上是一个大致的实现思路,具体实现过程中可能还需要根据你的业务需求进行一些调整和细化。希望对你有所帮助!如果你需要更详细的代码示例或者有其他问题,请提供更多细节,我将尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值