创意实现!在uni-app小程序商品详情页轮播中嵌入视频播放功能

背景介绍

通过uni-app框架实现商城小程序商品详情页的视频与图片轮播功能,以提升用户体验和增加商品吸引力。通过展示商品视频和图片,用户可以更全面地了解商品细节,从而提高购买决策的便利性和满意度。这种功能适用于各类商品,如服装、家具、电子产品等。通过本文的步骤,您将学会如何在商城小程序中利用uni-app框架实现商品详情页的视频与图片轮播功能。

代码组件

新建文件:productConSwiper.vue

html 复制代码
<template>
	<view class='product-bg'>
		<swiper :indicator-dots="indicatorDots" indicator-active-color="#E93323" :autoplay="autoplay"
			:circular="circular" :interval="interval" :duration="duration" @change="change">

			<swiper-item v-if="videoline">
				<view class="item">
					<view v-if="!controls" style="width:100%;height:100% ">
						<video id="myVideo" :src='videoline' objectFit="cover" controls style="width:100%;height:100% "
							show-center-play-btn show-mute-btn="true" auto-pause-if-navigate :custom-cache="false"
							:enable-progress-gesture="false" :poster="imgUrls[0]" @pause="videoPause"></video>
					</view>
					<view class="poster" v-if="controls">
						<image class="image" :src="imgUrls[0]"></image>
					</view>
					<view class="stop" v-if="controls" @tap="bindPause">
						<image class="image" src="../../static/images/stop.png"></image>
					</view>
				</view>
			</swiper-item>

			<block v-for="(item,index) in imgUrls" :key='index'>
				<swiper-item>
					<image :src="item" class="slide-image" />
				</swiper-item>
			</block>
		</swiper>
	</view>
</template>

<script>
	export default {
		props: {
			imgUrls: {
				type: Array,
				default: function() {
					return [];
				}
			},
			videoline: {
				type: String,
				value: ""
			}
		},
		data() {
			return {
				indicatorDots: true,
				circular: true,
				autoplay: true,
				interval: 3000,
				duration: 500,
				currents: "1",
				controls: true,
				isPlay: true,
				videoContext: null
			};
		},
		mounted() {
			if (this.videoline) {
				this.imgUrls.shift()
				this.videoContext = uni.createVideoContext('myVideo', this); // 创建videoContext
			}
		},
		methods: {
			videoPause(e) {},
			bindPause: function() { 
				if (this.videoContext) {
					this.videoContext.play(); // 调用play方法
					this.$set(this, 'controls', false);
					this.autoplay = false;
				}
			},
			change: function(e) {
				this.$set(this, 'currents', e.detail.current + 1);
			}
		}
	}
</script>

<style scoped lang="scss">
	.product-bg {
		width: 100%;
		height: 750rpx;
		position: relative;
	}

	.product-bg swiper {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.product-bg .slide-image {
		width: 100%;
		height: 100%;
	}

	.product-bg .pages {
		position: absolute;
		background-color: #fff;
		height: 34rpx;
		padding: 0 10rpx;
		border-radius: 3rpx;
		right: 30rpx;
		bottom: 30rpx;
		line-height: 34rpx;
		font-size: 24rpx;
		color: #050505;
	}

	#myVideo {
		width: 100%;
		height: 100%
	}

	.product-bg .item {
		position: relative;
		width: 100%;
		height: 100%;
	}

	.product-bg .item .poster {
		position: absolute;
		top: 0;
		left: 0;
		height: 750rpx;
		width: 100%;
		z-index: 9;
	}

	.product-bg .item .poster .image {
		width: 100%;
		height: 100%;
	}

	.product-bg .item .stop {
		position: absolute;
		top: 50%;
		left: 50%;
		width: 136rpx;
		height: 136rpx;
		margin-top: -68rpx;
		margin-left: -68rpx;
		z-index: 9;
	}

	.product-bg .item .stop .image {
		width: 100%;
		height: 100%;
	}
</style>

组件使用

javascript 复制代码
import productConSwiper from '@/components/productConSwiper';
components: {
			productConSwiper,
},

sliderImage是一个图片地址数组

productInfo.video是视频的地址

html 复制代码
<productConSwiper :imgUrls="sliderImage" :videoline="productInfo.video">
</productConSwiper>
相关推荐
脾气有点小暴18 小时前
scroll-view分页加载
前端·javascript·uni-app
脾气有点小暴21 小时前
uniapp自定义头部导航
前端·uni-app
一 乐1 天前
健身房预约|基于springboot + vue健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·小程序
前端 贾公子1 天前
[uniapp][swtich开关]阻止切换状态(类似阻止事件冒泡)
uni-app
豌豆学姐1 天前
Sora2 能做什么?25 秒视频生成 API 的一次接入实践
大数据·人工智能·小程序·aigc·php·开源软件
李慕婉学姐1 天前
【开题答辩过程】以《智慧校园创新互助小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·小程序
qq_12498707531 天前
基于微信小程序的校园跑腿系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计·计算机毕业设计
雪芽蓝域zzs1 天前
uniapp基于picker选择器实现年月日时分秒
uni-app
niucloud-admin1 天前
本地开发部署——uniapp端站点部署
uni-app
小小王app小程序开发1 天前
盲盒小程序一番赏特殊玩法超细分拆解:从底层逻辑到落地细节
大数据·小程序