小程序 Swiper 使用

Swiper

使用小程序的 swiper 组件,是能达到常规的 swiper 功能的,但是原生提供的样式有限,通常为图1、图2的样式。

通过样式的处理可达到图3的效果。示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// wxml
<swiper indicator-dots="{{true}}" previous-margin="8px" next-margin="8px" bindchange="bindSwiperChange">
<swiper-item wx:for="{{[1, 2, 3]}}" wx:key="{{index}}" class="swiperItem">
<view class="ruleCard {{currentSwiperItem < index && 'hiddenLeft'}} {{currentSwiperItem > index && 'hiddenRight'}}">
{{item}}
</view>
</swiper-item>
</swiper>
// js
Page({
data: {
currentSwiperItem: 0,
},
bindSwiperChange(e) {
this.setData({
currentSwiperItem: e.detail.current,
});
},
});
// wxss
swiper {
width: 100vw;
height: 363rpx;
}
.swiperItem {
display: flex;
justify-content: center;
width: calc(100vw - 16px) !important;
}
.ruleCard {
display: flex;
flex-direction: column;
justify-content: center;
width: calc(100vw - 40px);
height: 363rpx;
background: skyblue;
background-size: cover;
border-radius: 6px;
}
.hiddenLeft {
position: relative;
left: -10px;
}
.hiddenRight {
position: relative;
right: -10px;
}