uniapp实现app端图片+视频轮播

需求:swiper轮播里面,可能有图片也可能有视频。当swiper切换到视频时,视频以动画的样式展示(无按钮、进度条等默认播放控件),自动轮播取消,手动滑动切换取消。当视频播放完毕后,可以自动轮播,可以手动滑动切换。

找了个插件市场里的改的。(感谢大佬的插件!

第一步使用DCloud插件swiper-video-image - DCloud 插件市场

第二步:①video标签里加属性。实现隐藏按钮、进度条等默认播放控件

                        controls = "false"

              原swiperChange方法中添加判断,实现切换到视频时,自动轮播取消,手动滑动切换取消。

                    if (obj[this.typeName] === 'video'){
                       this.touch = true
                        this.autoplay = false
                    }    

                    swiper标签中加属性

                        :disable-touch = "touch"(是否禁止用户 touch 操作)

                        :autoplay="autoplay"(是否自动切换)

              ③使用@ended,用来判断视频当播放到末尾时触发事件,解开自动轮播,手动滑动切换。

                   @ended="videoEnded"

                videoEnded(){
                    this.touch = false
                    this.autoplay = true
                },

部分代码:

<template>
	<view class="content">
		<view class="screen-swiper-box">
			<swiper :current="index" @change="swiperChange" class="screen-swiper" indicator-dots="true" :circular="circular"
				:autoplay="autoplay" :interval="interval" duration="500" :style="[whStyle]" :disable-touch = "touch">
				<swiper-item v-for="(item, i) in list" :key="i" :id="i">
					<video v-if="item[typeName] == videoValue" :id="'myVideo-'+i" class="myVideo" :src="item[linkName]"
						controls = "false" objectFit="cover" enable-progress-gesture="false" show-loading="true"
						play-btn-position="center" show-fullscreen-btn="true" :style="[whStyle]"
						@click="swiperClick(item,i)" @ended="videoEnded"></video>
					<image v-if="item[typeName] == imgvalue" class="swiperImage" :src="item[linkName]" mode="scaleToFill"
						:style="[whStyle]" @click="swiperClick(item,i)">
					</image>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			index: 0, // 当前页
			videoCtx: '',
			touch:false,
		}
	},
	methods: {
		videoEnded(){
			this.touch = false
			this.autoplay = true
		},
		swiperChange(e) {
			// 获取上一个
			let obj = this.list[this.index]
			if (obj[this.typeName] == this.videoValue) {
				// console.log('暂停', this.index)
				this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
				this.videoCtx.pause()
			}

			// 获取当前
			this.index = e.detail.current // 更新当前index
			obj = this.list[this.index]
			if (obj[this.typeName] == this.videoValue && this.autoPlayVideo) {
				// console.log('播放', this.index)
				this.videoCtx = uni.createVideoContext('myVideo-' + this.index, this)
				this.videoCtx.play()
			}
			this.$emit('swiperChange', this.index)
			if (obj[this.typeName] === 'video'){
				this.touch = true
				this.autoplay = false
			}
		},
	},
}
</script>

                

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Uniapp实现App引导页轮播,可以按照以下步骤进行开发: 1. 创建一个页面作为App引导页,可以在该页面中添加多个图片作为轮播的引导页。 2. 在该页面中引入uni-ui组件库的swiper组件,用于实现轮播效果。 3. 在页面的data中定义一个数组,用于存储所有的引导页图片链接。 4. 在页面的mounted生命周期函数中,将所有引导页图片链接添加到data中定义的数组中。 5. 在swiper组件中使用v-for指令遍历data中的引导页图片链接,生成多个swiper-item组件,并设置每个swiper-item组件的背景图片为对应的引导页图片链接。 6. 在swiper组件中设置自动轮播和循环轮播等参数,以及添加分页器和左右箭头等控制器。 7. 最后,将该App引导页作为App的启动页,在App.vue中配置启动页为该页面即可。 示例代码如下: ```html <template> <view> <swiper :autoplay="true" :circular="true" :indicator-dots="true" :indicator-color="'#999'" :indicator-active-color="'#333'" :previous-margin="'50rpx'" :next-margin="'50rpx'"> <swiper-item v-for="(imgUrl, index) in imgList" :key="index"> <view class="swiper-item" :style="{backgroundImage: 'url(' + imgUrl + ')'}"></view> </swiper-item> </swiper> </view> </template> <script> import uniSwiperDot from '@/components/uni-swiper-dot/uni-swiper-dot.vue'; export default { components: { uniSwiperDot }, data() { return { imgList: [] // 存储所有的引导页图片链接 }; }, mounted() { this.imgList = [ 'https://xxx.com/guide_1.jpg', 'https://xxx.com/guide_2.jpg', 'https://xxx.com/guide_3.jpg' ]; // 添加所有的引导页图片链接 } }; </script> <style lang="scss" scoped> .swiper-item { width: 100%; height: 100vh; background-size: cover; background-position: center; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值