微信小程序swiper 视频中间大,两边小,轮播滑到中间视频自动播放组件教程

静态效果:

进入下面小程序可以体验效果,点击底部 看剧 栏目

一、创建小程序组件

二、代码

1、WXML

html 复制代码
<view class="swiper-wrapper" 
style="background-image:url(/asset/image/hot-banner.jpg);background-size: 100% 100%;">
    <swiper
    class="main-sw"
    autoplay="{{false}}"
    circular="{{true}}"
    interval="{{5000}}" 
    duration="{{500}}"
    current="{{posterList.length>2?1:0}}"
    previous-margin="255rpx"
    next-margin="255rpx"
    bindchange="swiperChange"
    >
    <block wx:for="{{posterList}}" wx:key="index">
        <swiper-item >
            <view class="swiper-item {{currentIndex==index?'swiper-item-active':'swiper-item-noactive'}}">
                <video
                class="vie" 
                id="{{'at_'+index}}"
                custom-cache="{{false}}"
                autoplay="{{currentIndex==index?true:false}}"
                data-index="{{index}}"
                bindplay="videoPlay"
                play-btn-position="center"
                show-bottom-progress="{{false}}"
                loop="{{currentIndex==index?true:false}}"
                enable-progress-gesture="{{false}}"
                show-fullscreen-btn="{{false}}"
                object-fit="fill" 
                src="{{item.url}}"
                poster=""/>
            </view>
        </swiper-item>
    </block>
    </swiper>
</view>

2、wxss

css 复制代码
.swiper-wrapper{
  flex: 1;
  width: 100%;
  height: 100%;
  margin: 0 auto;
  border-radius: 20rpx;
}
.main-sw{
  width: 100%;
  height: 430rpx;
}
 
.swiper-item{
  height: 450rpx;
  display: flex;
  align-items: center;
}
 
.swiper-item .vie{
  width: 180rpx;
  height: 300rpx;
  margin: 0 auto;
  border-radius: 20rpx;
  transition: all 0.6s;
}

.swiper-item-noactive{
    padding-top: 30rpx;
    transition: all 0.6s;
}

.swiper-item-active{
    transition: all 0.6s;
}

.swiper-item-active .vie{
  width: 100%;
  height: 360rpx;
  transition: all 0.6s;
}

3、JS

javascript 复制代码
// components/swiper-video/swiper-video.js
Component({
    lifetimes: {
        ready: function() {
        }
    },
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        currentIndex: 1,
        preIndex:-1,
        posterList: [
          {
            id: '1', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/02/15/15/BMjAyNDAyMTUxNTQ0NTNfMjI1MzQ2MDQxMV8xMjUwMDcyMDgyODZfMV8z_b_B5e4c6a553c4e284e7941a0ded6c00abb.mp4?tag=1-1708249770-unknown-0-p3ytzdyhr9-044ff73795d79af2&clientCacheKey=3xypn8bt5vm7w5q_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '2', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/01/30/16/BMjAyNDAxMzAxNjQwMzJfMzgyMzQ3MjQ1XzEyMzUyMTMwODU4NF8xXzM=_b_Be0f3bfddfb287f120ec484218179d214.mp4?tag=1-1708249795-unknown-0-4piuyvkt6d-783157ecbf33a1a3&clientCacheKey=3xpwz2fks5az6r4_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '3', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/02/17/14/BMjAyNDAyMTcxNDI0NDZfMTI5OTg4NTg4NV8xMjUxODQ5NTczNDRfMF8z_b_B3c596afe9e6ddc96e88d71351da052ac.mp4?tag=1-1708249822-unknown-0-ab5yaqiqon-f89388c9a0fbea5f&clientCacheKey=3x8669dhkxfztyc_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '4', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/02/17/17/BMjAyNDAyMTcxNzExMDRfMjM0NzM2OTY3OV8xMjUxOTkzNTUwOTlfMF8z_b_B531ec3a51ae00fdf805e892aefbf6733.mp4?tag=1-1708249852-unknown-0-jde8ia4ef8-80944aff380e6a62&clientCacheKey=3xzj5p23mfrrsm9_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '5', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/02/18/11/BMjAyNDAyMTgxMTAyMzdfMTYzNTI5NjEzNV8xMjUyNjE0MjQ4NTJfMF8z_b_B3da4064bfef058d0c2821f64f24d863e.mp4?tag=1-1708249875-unknown-0-oudstqms9g-770ac56e97209131&clientCacheKey=3xfywqaqhr4xav4_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '6', 
            url: 'https://tx2.a.kwimgs.com/upic/2023/12/08/14/BMjAyMzEyMDgxNDA1NThfMjEwNjQzMjExMV8xMTkxMTE3NDE1NzVfMV8z_b_Ba62e8a6ff0405a74160c6887700f4d6c.mp4?tag=1-1708177895-unknown-0-9bhguogrkc-94f990275f193667&clientCacheKey=3xsxvbspa44ezue_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '7', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/01/17/13/BMjAyNDAxMTcxMzMyMDZfMzg1MTI1NDMwMV8xMjIzMjA4MTg1MzJfMl8z_b_B64de3e477a3c56931c2e1c869455bb8a.mp4?tag=1-1708249935-unknown-0-8hoxqf5mqv-06a7d7fd75d38e4b&clientCacheKey=3xns4fd3r5iw3mq_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '8', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/01/02/19/BMjAyNDAxMDIxOTIxMDNfMzg4MTc4NzIwM18xMjEyMTY2NjYzOTNfMl8z_b_B7243f24b02219e4fafb2aa4e92cc5395.mp4?tag=1-1708249950-unknown-0-p2dy8mumai-fe39fb7133a179af&clientCacheKey=3xjxfvd5cnhip2u_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '9', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/02/16/17/BMjAyNDAyMTYxNzU3MTRfMTU2ODQ2MzM2Ml8xMjUxMTA1MDIwMDZfMV8z_b_B74927e5fb4232fc899c57f2e68fa5582.mp4?tag=1-1708249967-unknown-0-vrnqcigp6e-3d0aa458f1aaf315&clientCacheKey=3xu5uaiuju6y5xu_b.mp4&bp=10000&tt=b&ss=vp'
          },
          {
            id: '10', 
            url: 'https://tx2.a.kwimgs.com/upic/2024/01/01/11/BMjAyNDAxMDExMTAxMjhfMTk3MDc5MTUxMV8xMjExMDExOTE1ODVfMV8z_b_B6fc046ba1a3cc0b114ffb7a3feb87c68.mp4?tag=1-1708250014-unknown-0-glxnbdhuvo-79c4b0891fd217ee&clientCacheKey=3x43rid7dz3y7pk_b.mp4&bp=10000&tt=b&ss=vp'
          },
        ]
    },

    /**
     * 组件的方法列表
     */
    methods: {
        //视频切换
        swiperChange(event){
            if(this.data.preIndex>-1){
                var cxt = wx.createVideoContext('at_'+this.data.preIndex, this);
                //停止前一个视频的播放
                cxt.stop();
                //将视频重头开始播放
                cxt.seek(10000);
            }
            let {current} = event.detail;
            var cxt = wx.createVideoContext('at_'+current, this);
            cxt.play();
            this.setData({
                currentIndex: current,
                preIndex: current
            })
        },
        //视频播放
        videoPlay(e){
            this.setData({
                preIndex: e.currentTarget.dataset.index
            })
        }
    }
})
相关推荐
计算机徐师兄2 小时前
Java基于SSM框架的无中介租房系统小程序【附源码、文档】
java·微信小程序·小程序·无中介租房系统小程序·java无中介租房系统小程序·无中介租房微信小程序
源码哥_博纳软云2 小时前
JAVA智慧养老养老护理帮忙代办陪诊陪护小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
说私域2 小时前
私域电商逆袭密码:AI 智能名片小程序与商城系统如何梦幻联动
人工智能·小程序
美美的海顿8 小时前
springboot基于Java的校园导航微信小程序的设计与实现
java·数据库·spring boot·后端·spring·微信小程序·毕业设计
说私域12 小时前
无人零售及开源 AI 智能名片 S2B2C 商城小程序的深度剖析
人工智能·小程序·零售
罗狮粉 9913 小时前
docker部署微信小程序自动构建发布和更新
docker·微信小程序·notepad++
Kika写代码1 天前
【微信小程序】页面跳转基础 | 我的咖啡店-综合实训
服务器·微信小程序·小程序
源码哥_博纳软云1 天前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
禾高网络1 天前
租赁小程序成品|租赁系统搭建核心功能
java·人工智能·小程序
YUJIAN。1 天前
使用uniapp开发微信小程序-框架搭建
微信小程序·小程序·uni-app