当前位置: 首页 > news >正文

微信小程序(原生)使用Swiper实现(商品详情)视频和图片轮播(仿京东/淘宝商品详情头部视频+图片轮播)

一、需求

1、如果第一是视频,不进行自动轮播
2、可以手动滑动切换
3、点击播放视频,也可以手动滑动切换
4、视频播放完后,自动轮播
5、视频可以点击暂停和全屏播放

二、最终效果

在这里插入图片描述

三、源码

播放icon使用了TDesign组件库

1、wxml

<swiper class="detail-banner" wx:if="{{details.images.length > 0}}" indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" circular="{{circular}}" indicator-color="{{indicatorColor}}" indicator-active-color="{{indicatorActiveColor}}" style="background: #fff;"><swiper-item wx:for="{{bannerMsg}}" wx:for-item="item" wx:key="index"><view class='videocover' data-id="{{index}}" wx:if="{{item.type==2}}"><view class='videocoverbg'></view><t-icon name="play-circle" size="100rpx" class="playIcon" bindtap="videoPlay" wx:if="{{!controls}}" /></view><view wx:if="{{item.type==2}}"><video class='box-w block' id="video" src="{{item.url}}" show-center-play-btn="{{false}}" objectFit="cover" bindended="endPlay" controls="{{controls}}"></video></view><image class="detail-banner-img" src="{{item.url}}" data-src="{{item.url}}" wx:if="{{item.type==1}}"></image></swiper-item></swiper>

2、wxss

 .detail-banner {width: 100%;height: 500rpx;padding: 0 0 10rpx 0;
}.detail-banner-img {width: 100%;height: 100%;
}/* video */.box-w {width: 100%;height: 500rpx;
}.videocover {width: 100%;overflow: hidden;
}.videocoverbg {position: absolute;left: 0;top: 0;right: 0;bottom: 0;background: rgba(0, 0, 0, 0.3);
}.playIcon {position: absolute;top: 50%;z-index: 2;left: 50%;width: 100rpx;height: 100rpx;background-color: #fff;border-radius: 50%;transform: translate(-50%, -50%);
}.videocover .cover {width: 100%;
}

3、js

Page({data: {details: {}, // 商品详情信息indicatorDots: true, // 是否显示面板指示点autoplay: false, // 自动播放interval: 3000, // 自动切换时间间隔duration: 400, // 滑动动画时长circular: true, //是否循环 是否采用衔接滑动indicatorColor: "lightgray", //指示点颜色indicatorActiveColor: "red", //当前选中的指示点颜色controls: false, // 是否显示播放icon},// 获取商品详情getDetail(spuId) {const selectedAddr = wx.getStorageSync('addressMsg')const storeId = selectedAddr.storeIdconst selectedAddrStr = selectedAddr.streetPromise.all([fetchGood(spuId, storeId), fetchActivityList(spuId, storeId)]).then((res) => {const [details, activityData] = res;const {video,images} = details;let bannerMsg = []bannerMsg = bannerMsg.concat(images)bannerMsg = bannerMsg.map(val => {return {type: 1,url: val}})if (video) {bannerMsg = [{ type: 2, url: video }, ...bannerMsg]}// console.log('bannerMsg', bannerMsg, images[0]);this.setData({details,bannerMsg});});},//预览图片previewImage(e) {console.log('预览图片', e)const current = e.currentTarget.dataset.src;wx.previewImage({current, // 当前显示图片的http链接  urls: this.data.bannerMsg// urls: this.data.imgUrls // 需要预览的图片http链接列表  })},// 播放videoPlay() {console.log("开始播放")this.setData({autoplay: false})let videoplay = wx.createVideoContext("video");videoplay.play()this.setData({controls: true})},// 结束播放endPlay() {console.log("结束播放")this.setData({controls: false,autoplay: true})},onShow() {this.getDetail();}
});

四、其他文章

基于ElementUi或Antd再次封装基础组件文档

vue3+ts基于Element-plus再次封装基础组件文档

http://www.lryc.cn/news/165388.html

相关文章:

  • 关于for in 循环会遍历原型链上的属性的问题
  • 冠达管理:人民币升值板块个股?
  • 27.EI文章复现《高比例清洁能源接入下计及需求响应的配电网重构》
  • mysql的索引结构
  • SMT生产中基板的机械清洁处理法有哪些
  • 微服务面试题
  • LeetCode 1132.申请的报告2
  • 室内探索无人机,解决复杂环境下的任务挑战!
  • 操作指南 | 如何参与Moonbeam投票委托
  • xxl-job中多节点分片的时候如何在linux服务器开启多个执行器实例?
  • springboot三种注入方式
  • 信息化发展38
  • PMP含金量再升级!北京上海等地可评职称!
  • 动态调用微服务
  • 什么是字符集什么是字符编码
  • Python小项目之Tkinter应用】随机点名/抽奖工具大优化:新增查看历史记录窗口!语音播报功能!修复预览文件按钮等之前版本的bug!
  • mysql drop table 死锁
  • Git零基础入门(Linux版)
  • 二维多孔介质图像的粒度分布研究(Matlab代码实现)
  • 文盘Rust——子命令提示,提高用户体验 | 京东云技术团队
  • 同源策略简介
  • 数据量大,分析困难?试试pandas随机抽样
  • stm32---外部中断
  • 电子企业MES管理系统实施的功能和流程有哪些
  • 代码随想录二刷day24
  • 谷粒商城篇章6 ---- P193-P210 ---- 异步线程池商品详情【分布式高级篇三】
  • gcc中的cc1 collect2
  • 学习day59
  • Go Tip02 指针类型 、值类型和引用类型 、标识符的命名规范
  • CSS中如何实现文字跑马灯效果?