微信小程序——组件之swiper

本文介绍了如何使用微信小程序的Swiper组件创建图片轮播效果,包括设置自动切换、指示点颜色、动画时长等属性,并提供了一个简单的示例及实际效果展示。

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

大家看到许多网页的首页面都会有图片的轮播,图片轮播:既能通过图片增加整个页面的美观程度,又能让客户一眼看出网页想要表达的内容。那么,今天我们就通过swiper实现图片的轮播效果,简单又好看。

/**wxml**/

<swiper 

indicator={{是否显示面板指示点}}

indicator-color={{指示点颜色}}

indicator-active-color={{当前选中的指示点颜色}}

autoplay={{是否自动切换}}

current={{当前所在滑块的 index}}

current-item-id={{当前所在滑块的 item-id ,不能与 current 被同时指定}}

interval={{自动切换时间间隔}}

duration={{滑动动画时长}}

circular={{是否采用衔接滑动}}

vertical={{滑动方向是否为纵向}}

previous-margin={{前边距,可用于露出前一项的一小部分,接受 px 和 rpx 值}}

next-margin={{后边距,可用于露出后一项的一小部分,接受 px 和 rpx 值}}

display-multiple-items={{同时显示的滑块数量}}

skip-hidden-item-layout={{是否跳过未显示的滑块布局,设为 true 可优化复杂情况下的滑动性能,但会丢失隐藏状态滑块的布局信息}}

bindchange={{current 改变时会触发 change 事件,event.detail = {current: current, source: source}}}

bindanimationfinish={{动画结束时会触发 animationfinish 事件,event.detail = {current: current, source: source}}}>

</swiper>



/**.js**/

page({

data:{//默认值

indicatordots:false,

indicatorcolor:rgba(0,0,0,.3),

indicatoractivecolor:#000000,

autoplay:false,

current:0,

currentitemid:"",

interval:5000,

duration:500,

circular:false,

vertical:false,

previousmargin:0px,

nextmargin:0px,

displaymultipleitems:1,

skiphiddenitemlayout:false

}

})

下面举一个小例子,只用了上面的部分属性。

<!--index.wxml-->
<swiper indicator-dots="{{indicatorDots}}"
autoplay="{{autoplay}}" circular="{{circular}}" interval="{{interval}}" duration="{{duration}}">
<block wx:for="{{imgUrls}}">
<swiper-item>
<image src="{{item}}"/>
</swiper-item>
</block>
</swiper>


//index.js
Page({
data: {
imgUrls: [
'../images/tu1.jpg',
'../images/tu2.jpg',
'../images/tu3.jpg'
],
indicatorDots: false,
autoplay:true,
circular:true,
interval: 5000,
duration: 1000
}

})

效果图如下:


这是一个自动切换和衔接滑动的图片轮播,按照读者自己的需求,可以加上上述的其他属性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值