前言:小程序官方swiper組件并未提供帶左右箭頭功能,但有些時候還是想把左右箭頭添加上,今天連勝老師就給大家分享一下自己的實現(xiàn)方式。
思路很簡單:在swiper組件內(nèi)部添加兩個image組件,綁定點擊事件,動態(tài)改變swiper中的current值。不廢話,主要看代碼:
WXML:
WXSS:
-
swiper{
-
-
position:relative;
-
-
height:300rpx;
-
-
}
-
-
swiperimage{
-
-
display:block;
-
-
width:100%;
-
-
height:300rpx;
-
-
cursor:pointer;
-
-
}
-
-
swiper.arrow{
-
-
width:30rpx;
-
-
height:46rpx;
-
-
}
-
-
swiper.prev{
-
-
position:absolute;
-
-
left:0;
-
-
top:50%;
-
-
transform:translate(0,-50%);
-
-
cursor:pointer;
-
-
}
-
-
swiper.next{
-
-
position:absolute;
-
-
right:0;
-
-
top:50%;
-
-
transform:translate(0,-50%);
-
-
}
-
-
JS:
-
-
//index.js
-
-
Page({
-
-
data: {
-
-
swiper: {
-
-
imgUrls: [
-
-
'/images/swiper01.jpg',
-
-
'/images/swiper02.jpg',
-
-
'/images/swiper03.jpg'
-
-
],
-
-
indicatorDots:true,
-
-
autoplay:false,
-
-
interval:5000,
-
-
duration:1000,
-
-
current:0,
-
-
}
-
-
},
-
-
prevImg:function(){
-
-
varswiper =this.data.swiper;
-
-
varcurrent = swiper.current;
-
-
swiper.current= current>0? current-1: swiper.imgUrls.length-1;
-
-
this.setData({
-
-
swiper: swiper,
-
-
})
-
-
},
-
-
nextImg:function() {
-
-
varswiper =this.data.swiper;
-
-
varcurrent = swiper.current;
-
-
swiper.current= current < (swiper.imgUrls.length-1) ? current +1:0;
-
-
this.setData({
-
-
swiper: swiper,
-
-
})
-
-
}
-
-
})
|