-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswipertest.js
262 lines (251 loc) · 7.99 KB
/
swipertest.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
class Swiper {
constructor(ele, config = {}) {
var container = document.querySelector(ele),
bannerWrap = container.querySelector('.swiper-wrap'),
btnPrev = container.querySelector('.swiper-btn-prev'),
btnNext = container.querySelector('.swiper-btn-next'),
lis = container.querySelectorAll('.swiper-wrap li'),
delay = config.delay || 2000,
autoPlay = config.autoPlay === undefined ? true : config.autoPlay,
p1 = lis[0],
p2 = lis[1],
p3 = lis[2],
p4 = lis[3],
p5 = lis[4],
p6 = lis[5],
prev = null,
next = null,
maxNum = lis.length - 1,
dots = {},
timer = 0,
dotIndex = 0,
isAddDot = false,
startP, disX, disY, index;
if (lis.length < 3) {
throw ("image number need 3+");
}
if (config.navigator) {
if (config.navigator.prev) {
prev = document.querySelector(config.navigator.prev);
}
if (config.navigator.next) {
next = document.querySelector(config.navigator.next);
}
}
if (lis.length > 6) {
p5 = lis[lis.length - 2];
p6 = lis[lis.length - 1];
for (let i = 0; i < lis.length; i++) {
lis[i].classList.add("p4");
}
p1.className = "p1";
p2.className = "p2";
p3.className = "p3";
p5.className = "p5";
p6.className = "p6";
bannerWrap.classList.add("swiper-6");
} else {
for (let i = 0; i < lis.length; i++) {
lis[i].classList.add(`p${i + 1}`);
}
bannerWrap.classList.add(`swiper-${lis.length}`);
}
// 设置点
if (config.pagination) {
let p = container.querySelector(config.pagination);
for (let i = 0, len = lis.length; i < len; i++) {
let li = document.createElement("li");
p.appendChild(li);
}
dots = p.querySelectorAll('li');
dots[0].className = "active";
}
bannerWrap.classList.add("transition");
// 图片移动,dire判断是否滑动过
function move(dire) {
var arrCls = []; // 保存类名,用来赋值
dire = dire || false;
dotIndex++;
// 得到所有类名
for (var i = 0; i < lis.length; i++) {
arrCls.push(lis[i].className);
}
// 循环赋值类名
for (var i = 0; i < lis.length; i++) {
if (dire) {
// 判断左滑
if (dire < 0) {
index = i - 1; // 得到最前面元素的上一个, 自己脑补过程
if (index < 0) {
index = maxNum;
}
} else if (dire > 0) { // 判断右滑
index = i + 1; // 下一个
if (index > maxNum) {
index = 0;
}
if (!isAddDot) { // 点导航下一个
dotIndex -= 2;
isAddDot = true;
}
}
}
// 如果没有参数,正常循环
if (!dire) {
index = i - 1;
if (index < 0) {
index = maxNum;
}
}
lis[i].className = arrCls[index];
}
if (dotIndex > maxNum) {
dotIndex = 0;
} else if (dotIndex < 0) {
dotIndex = maxNum;
}
isAddDot = false; // 判断是否加过
setDotActive(dotIndex);
}
function setDotActive(index) {
if (config.pagination) {
for (var i = 0; i < dots.length; i++) {
dots[i].className = '';
}
dots[index].className = 'active';
}
}
function setTimer() {
if (autoPlay) {
timer = setInterval(move, delay);
}
}
function addTransition() {
bannerWrap.classList.add('transition');
}
function removeTransition() {
bannerWrap.classList.remove('transition');
}
function picTransform(x) {
var p1 = container.querySelector('.p1'),
p2 = container.querySelector('.p2'),
p3 = container.querySelector('.p3'),
p4 = container.querySelector('.p4'),
p5 = container.querySelector('.p5'),
p6 = container.querySelector('.p6');
var percentV2 = Math.floor(p2.offsetWidth * 0.15), // p2 的百分比转px
percentV3 = Math.floor(p3.offsetWidth * 0.26); // p3
var number = lis.length > 6 ? 6 : lis.length;
if (x < -7) {
x = -7;
} else if (x > 12) {
x = 12
}
p1.style.transform = 'translate3d(' + x + 'px,0,100px) scale(.5)';
p2.style.transform = 'translate3d(' + (percentV2 - x) + 'px,0,90px) scale(.4)';
switch (number) {
case 3:
p3.style.transform = 'translate3d(' + (-percentV2 - x) + 'px,0,90px) scale(.4)';
break;
case 4:
p4.style.transform = 'translate3d(' + (-percentV2 - x) + 'px,0,90px) scale(.4)';
break;
case 5:
p3.style.transform = 'translate3d(' + (percentV3 - (x * 2)) + 'px,0,80px) scale(.3)';
p4.style.transform = 'translate3d(' + (-percentV3 - (x * 4)) + 'px,0,80px) scale(.3)';
p5.style.transform = 'translate3d(' + (-percentV2 - (x * 2)) + 'px,0,90px) scale(.4)';
break;
case 6:
p3.style.transform = 'translate3d(' + (percentV3 - (x * 2)) + 'px,0,80px) scale(.3)';
p4.style.transform = 'translate3d(' + x + 'px,0,80px) scale(.3)';
p5.style.transform = 'translate3d(' + (-percentV3 - (x * 4)) + 'px,0,80px) scale(.3)';
p6.style.transform = 'translate3d(' + (-percentV2 - (x * 2)) + 'px,0,90px) scale(.4)';
break;
}
}
function disabledPrev(e) {
e.preventDefault();
}
setTimer()
bannerWrap.addEventListener('touchstart', function (e) {
document.addEventListener("touchmove", disabledPrev(e), { passive: false }); // 禁止滑动后退
removeTransition();
startP = e.changedTouches[0].clientX;
clearInterval(timer);
}, false);
bannerWrap.addEventListener('touchmove', function (e) {
var movex = e.changedTouches[0].clientX;
disX = movex - startP; // 得到滑动的距离
picTransform(disX / 20); // 让图片缓慢滑动
}, false);
bannerWrap.addEventListener('touchend', function (e) {
document.removeEventListener("touchmove", disabledPrev(e), { passive: false }); // 禁止滑动后退
addTransition();
var p1 = container.querySelector('.p1'),
p2 = container.querySelector('.p2'),
p3 = container.querySelector('.p3'),
p4 = container.querySelector('.p4'),
p5 = container.querySelector('.p5'),
p6 = container.querySelector('.p6');
// 清除行内样式,否则类名的样式不生效
if (p1) { p1.style.cssText = ''; }
if (p2) { p2.style.cssText = ''; }
if (p3) { p3.style.cssText = ''; }
if (p4) { p4.style.cssText = ''; }
if (p5) { p5.style.cssText = ''; }
if (p6) { p6.style.cssText = ''; }
// 防止点击时也会触发
if (disX) {
move(disX);
disX = false;
}
setTimer()
}, false);
bannerWrap.addEventListener("mouseenter", function () {
clearInterval(timer);
});
btnPrev.addEventListener("mouseenter", function () {
clearInterval(timer);
});
btnNext.addEventListener("mouseenter", function () {
clearInterval(timer);
});
btnPrev.addEventListener("click", function () {
clearInterval(timer);
});
btnNext.addEventListener("click", function () {
clearInterval(timer);
});
bannerWrap.addEventListener("mouseleave", function () {
setTimer()
});
btnPrev.addEventListener("mouseleave", function () {
setTimer()
});
btnNext.addEventListener("mouseleave", function () {
setTimer()
});
if (prev) {
prev.onclick = function () {
move(1);
};
prev.onmousedown = function () {
clearInterval(timer);
}
prev.onmouseup = function () {
setTimer()
}
}
if (next) {
next.onmousedown = function () {
clearInterval(timer);
}
next.onmouseup = function () {
setTimer()
}
next.onclick = function () {
move(-1);
};
}
}
}