微信小程序之卡片层叠滑动效果
代码:
js:
// index/gun/jsSwiper2/jsSwiper2.js
Page({
/**
* 页面的初始数据
*/
data: {
startX: 0,
endX: 0,
iCenter: 3,
datas: [{
id: 1,
zIndex: 2,
opacity: 0.2,
left: 40,
iamge: "../../images/1.jpg",
animation: null
},
{
id: 2,
zIndex: 4,
opacity: 0.4,
left: 80,
iamge: "../../images/2.jpg",
animation: null
},
{
id: 3,
zIndex: 6,
opacity: 0.6,
left: 120,
iamge: "../../images/3.jpg",
animation: null
},
{
id: 4,
zIndex: 8,
opacity: 1,
left: 160,
iamge: "../../images/4.jpg",
animation: null
},
{
id: 5,
zIndex: 6,
opacity: 0.6,
left: 200,
iamge: "../../images/5.jpg",
animation: null
},
{
id: 6,
zIndex: 4,
opacity: 0.4,
left: 240,
iamge: "../../images/6.jpg",
animation: null
},
{
id: 7,
zIndex: 2,
opacity: 0.2,
left: 280,
iamge: "../../images/7.jpg",
animation: null
},
],
order: []
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.__set__();
this.move();
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
move: function () {
var datas = this.data.datas;
/*图片分布*/
for (var i = 0; i < datas.length; i++) {
var data = datas[i];
var animation = wx.createAnimation({
duration: 200
});
animation.translateX(data.left).step();
this.setData({
["datas[" + i + "].animation"]: animation.export(),
["datas[" + i + "].zIndex"]: data.zIndex,
["datas[" + i + "].opacity"]: data.opacity,
})
}
},
/**左箭头 */
left: function () {
//
var last = this.data.datas.pop(); //获取数组的最后一个
this.data.datas.unshift(last);//放到数组的第一个
var orderFirst = this.data.order.shift();
this.data.order.push(orderFirst);
this.move();
},
/** */
right: function () {
var first = this.data.datas.shift(); //获取数组的第一个
this.data.datas.push(first);//放到数组的最后一个位置
var orderLast = this.data.order.pop();
this.data.order.unshift(orderLast);
this.move();
},
/**点击某项 */
choose: function (e) {
var that = this;
var id = e.currentTarget.dataset.id;
var order = that.data.order;
var index = 0;
for (var i = 0; i < order.length; i++) {
if (id == order[i]) {
index = i;
break;
}
}
if (index < that.data.iCenter) {
for (var i = 0; i < that.data.iCenter - index; i++) {
this.data.datas.push(this.data.datas.shift()); //获取第一个放到最后一个
this.data.order.unshift(this.data.order.pop());
// this.right()
}
} else if (index > that.data.iCenter) {
for (var i = 0; i < index - that.data.iCenter; i++) {
this.data.datas.unshift(this.data.datas.pop()); //获取最后一个放到第一个
this.data.order.push(this.data.order.shift());
// this.left();
}
}
this.move();
},
/**新的排列复制到新的数组中 */
__set__: function () {
var that = this;
var order = that.data.order;
var datas = that.data.datas;
for (var i = 0; i < datas.length; i++) {
that.setData({
["order[" + i + "]"]: datas[i].id
})
}
},
//手指触发开始移动
moveStart: function (e) {
console.log(e);
var startX = e.changedTouches[0].pageX;
this.setData({
startX: startX
});
},
//手指触摸后移动完成触发事件
moveItem: function (e) {
console.log(e);
var that = this;
var endX = e.changedTouches[0].pageX;
this.setData({
endX: endX
});
//计算手指触摸偏移剧距离
var moveX = this.data.startX - this.data.endX;
//向左移动
if (moveX > 20) {
this.left();
}
if (moveX < -20) {
this.right();
}
},
})
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
wxml:
1
2
3
4
5
6
7
8
9
10
11
12
13
wxss:
.teachers_banner {
width: 100%;
height: 650px;
background-size: cover;
position: relative;
overflow: hidden;
}
.teachers_b {
position: relative;
margin-top: 80px;
}
#slide {
margin: 0 auto;
width: 100%;
height: 350px;
position: relative;
}
image {
width: 400rpx;
height: 550rpx;
}
#slide li {
position: absolute;
width: 400rpx;
display: -webkit-box;
display: -webkit-flex;
display: flex;
align-items: flex-start;
-webkit-box-align: flex-start;
-webkit-align-items: flex-start;
overflow: hidden;
box-shadow: 0 0 20px #1d374d;
}
#slide li img {
width: 100%;
height: 100%;
}
.slide_right {
padding: 40px;
-webkit-box-flex: 1;
-webkit-flex: 1;
flex: 1;
min-width: 0;
}
.slide_right dl {
padding-top: 10px;
}
.arrow .prev, .arrow .next {
position: absolute;
width: 50px;
top: 38%;
z-index: 9;
font: 700 96px 'simsun';
opacity: 0.3;
color: #fff;
cursor: pointer;
}
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
54
55
56
57
58
59
60
61
62
63
64
65
66
效果:
1.左右滑动时,向相应方向移动一个卡片位置;
2.点击某一项时,将点击项位置移动到中间位置;
————————————————
版权声明:本文为CSDN博主「CodeHunter_qcy」的原创文章,遵循 CC 4.0 BY 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43390235/article/details/90754794