微信小程序滑块视图容器swiper类似于安卓的ViewPager,但是微信小程序的swiper相比于安卓的ViewPager要容易实现一点,下面将为大家介绍swiper的一些使用方法。
主要属性:
属性名 | 类型 | 默认值 | 说明 |
Indicator-dots | Boolean | False | 是否显示面板指示点 |
Autoplay | Boolean | False | 是否主动切换 |
Current | Number | 0 | 当前所在页码的index |
Interval | Number | 5000 | 自动切换时间间隔 |
Duration | Number | 1000 | 滑动动画时长 |
Bindchange | eventHandle | Current改变是会触发change事件,event.detail={current:current} |
数据可以通过抽到js文件中的data进行数据绑定,对bindchange进行监听,在js中进行业务处理。
以下是效果图:
以下是wxml代码:
<!--是否显示圆点,自动播放, 间隔时间, 监听滚动和点击事件-->
<swiper indicator-dots="true" autoplay="true" duration="1000" bindchange="listenSwiper" >
<!--swiper-item只能包含一个节点再多会自动删除-->
<swiper-item>
<view style="background: red; height: 150px"></view>
</swiper-item>
<swiper-item>
<view style="background: green; height: 150px"></view>
</swiper-item>
<swiper-item>
<view style="background: blue; height: 150px"></view>
</swiper-item>
</swiper>
以下是js代码:
Page({ data:{ // text:"这是一个页面" },
/**
* 这里处理滚动事件处理
*/ listenSwiper:function(e) {
//打印信息
console.log(e)
},
onLoad:function(options){
// 页面初始化 options为页面跳转所带来的参数
},
onReady:function(){
// 页面渲染完成
},
onShow:function(){
// 页面显示
},
onHide:function(){
// 页面隐藏
},
onUnload:function(){
// 页面关闭
} })