五分钟掌握微信小程序轮播图
来源:互联网 发布:给文件加密的算法 编辑:程序博客网 时间:2024/06/06 01:38
微信小程序轮播图实现,比Android 轮播图来说,显得轻松多了。
微信小程序提供swiper组件,官网api提供的swiper滑块视图容器。
current 改变时会触发 change 事件,event.detail = {current: current, source: source}
从公共库v1.4.0开始,change事件返回detail中包含一个source字段,表示导致变更的原因,可能值如下:
autoplay 自动播放导致
swiper
变化;touch 用户划动引起swiper变化;
其他原因将用空字符串表示。
注意:其中只可放置<swiper-item/>
组件,否则会导致未定义的行为。
swiper-item
仅可放置在<swiper/>
组件中,宽高自动设置为100%。
index.wxss
swiper {
height: 421.5rpx;
}
swiper-item image {
width: 100%;
height: 100%;
}
.swiper-container{
position: relative;
}
.swiper-container .swiper{
height: 300rpx;
}
.swiper-container .swiper .img{
width: 100%;
height: 100%;
}
index.js
Page({
data: {
swiperCurrent: 0,
indicatorDots: true,
autoplay: true,
interval: 3000,
duration: 800,
circular:true,
imgUrls: [
'https://p3.pstatp.com/large/43700001e49d85d3ab52',
'https://p3.pstatp.com/large/39f600038907bf3b9c96',
'https://p3.pstatp.com/large/31fa0003ed7228adf421'
],
links:[
'../user/user',
'../user/user',
'../user/user'
]
},
//轮播图的切换事件
swiperChange: function (e) {
this.setData({
swiperCurrent: e.detail.current
})
},
//点击指示点切换
chuangEvent: function (e) {
this.setData({
swiperCurrent: e.currentTarget.id
})
},
//点击图片触发事件
swipclick: function (e) {
console.log(this.data.swiperCurrent);
wx.switchTab({
url: this.data.links[this.data.swiperCurrent]
})
}
})
index.wxml
<view class="swiper-container">
<swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}" circular="{{duration}}" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">
<block wx:for="{{imgUrls}}" wx:key="unique">
<swiper-item>
<image src="{{item}}" class="img" bindtap="swipclick" />
</swiper-item>
</block>
</swiper>
</view>
重要一点是图片的点击事件。bindtap="swipclick"
swipclick: function (e) {
console.log(this.data.swiperCurrent);
wx.switchTab({
url: this.data.links[this.data.swiperCurrent]
})
}
【END】
往期热文推荐
程序员月薪多少才不会焦虑
成大事者不纠结,做好自己就好
为什么有的人工作多年还是老样子
你必须非常努力,才能看起来毫无费力
跟优秀的人一起共事,我们真的会越来越优秀
更多精彩内容,长按二维码,识别并关注!
- 五分钟掌握微信小程序轮播图
- JUnit五分钟掌握
- 30分钟掌握STL系列(五)
- Redis数据结构详解,五种数据结构分分钟掌握
- Redis数据结构详解,五种数据结构分分钟掌握
- 五分钟
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 30分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 三十分钟掌握STL
- 使用Random对象生成n个[a,b]区间的随机数
- Poj-1166-The Clocks-枚举
- Java学习笔记(2)
- 使用有道API完成在线翻译功能
- 多少人的感情败给了这四个字
- 五分钟掌握微信小程序轮播图
- MySql关键字汇总
- L3
- 内存溢出和内存泄漏的区别、产生原因以及解决方案
- 单片机温习
- 模式匹配:KMP算法
- 基于文本和图片的商品分类
- 图像处理基础知识
- Hyperledger Fabric 1.0架构及原理