微信小程序 swiper 层叠轮播图

本文详细介绍了一种在微信小程序中实现轮播图组件的方法,包括wxml布局、wxss样式及js逻辑处理。通过设置indicatorDots、autoplay等属性,实现图片自动轮播效果,并通过swiper组件和bindchange事件实现图片切换时的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

wxml:

<view class="banner-swiper">

<swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" current='{{swiperCurrent}}' 

indicator-color="{{beforeColor}}" indicator-active-color="{{afterColor}}" circular='{{circular}}' 

previous-margin="{{previousmargin}}" next-margin="{{nextmargin}}" bindchange="swiperChange" >

<block wx:for="{{arr}}" wx:key="key">

<swiper-item>

<image src="{{item.images}}" class="slide-image{{index == swiperCurrent ? ' active' : ''}}" 

bindchange="chuangEvent" id="{{index}}"></image>

</swiper-item>

</block>

</swiper>

</view>

wxss:

.banner-swiper {

width: 100%;

height: 500rpx;

overflow: hidden;

}

 

swiper {

display: block;

height: 500rpx;

position: relative;

}

 

.slide-image {

width: 96%;

display: block;

margin: 0 auto;

height: 450rpx;

margin-top:25rpx;

}

.active{

margin-top:0rpx;

height: 500rpx;

}

js:

 

Page({

data: {

//轮播图

swiperCurrent:1,

arr: [{

images: 'images/1.jpg'

},

{

images: 'images/5.jpg'

},

{

images: 'images/3.jpg'

},

{

images: 'images/4.jpg'

}

]

indicatorDots: true,

autoplay: true,

interval: 2000,

duration: 1000,

circular: true,

beforeColor: "white",//指示点颜色 

afterColor: "coral",//当前选中的指示点颜色 

previousmargin:'30px',//前边距

nextmargin:'30px',//后边距

},

//轮播图的切换事件 

swiperChange: function (e) {

console.log(e.detail.current);

this.setData({

swiperCurrent: e.detail.current  //获取当前轮播图片的下标

})

},

//滑动图片切换 

chuangEvent: function (e) { 

this.setData({

swiperCurrent: e.currentTarget.id

})

},

})

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值