Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
384 lines (363 loc) · 11 KB
/
Copy pathindex.js
File metadata and controls
384 lines (363 loc) · 11 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
root.Swiper = factory();
}
})(window, function () {
function lightJQ() {
var supportsPassive = false,
isLoad = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function () {
supportsPassive = true;
}
});
window.addEventListener('test', null, opts);
} catch (e) {}
var $ = function (elem) {
return typeof elem === 'string' ? document.querySelectorAll(elem) : elem;
};
$.ready = function (callback) {
document.addEventListener('DOMContentLoaded', function () {
if (isLoad) return;
isLoad = true;
callback();
});
window.onload = function () {
if (isLoad) return;
isLoad = true;
callback();
};
};
HTMLElement.prototype.addClass = function (className) {
this.className += ' ' + className;
};
HTMLElement.prototype.removeClass = function (className) {
var classes = this.className.split(/\s/);
var idx = classes.indexOf(className);
if (idx != -1) {
for (var i = 0; i < classes.length;) {
if (classes[i] === className) {
classes.splice(i, 1);
continue;
}
++i;
}
this.className = classes.join(' ');
}
};
HTMLElement.prototype.on = function (event, callback) {
this.addEventListener(event, callback, supportsPassive ? {
passive: true
} : false);
};
HTMLElement.prototype.find = function (elem) {
// 这里会有 querySelector 的坑,使用时注意
return this.querySelectorAll(elem);
};
NodeList.prototype.find = function (elem) {
return [].reduce.call(this, function (prev, next) {
var nodes = [].slice.call(next.querySelectorAll(elem));
return prev.concat(nodes);
}, []);
};
HTMLElement.prototype.width = function (value) {
if (value) {
this.style.width = value + 'px';
} else {
return this.offsetWidth;
}
};
HTMLElement.prototype.height = function (value) {
if (value) {
this.style.height = value + 'px';
} else {
return this.offsetHeight;
}
};
HTMLElement.prototype.css = function (property, value) {
this.style[property] = value;
};
return $;
}
var $ = lightJQ();
function Swiper(opts) {
var self = this instanceof Swiper ? this : Object.create(self);
// container容器 dom对象
typeof opts.el === 'string' ? self.el = $(opts.el)[0] : self.el = opts.el;
// 滚动方向
self.direction = opts.direction || 'horizontal';
// 是否居中
self.centeredSlides = !!opts.centeredSlides;
// autoplay -> number
self.autoplay = null;
// slide的间隔margin
self.spaceBetween = opts.spaceBetween;
// 起始的slide索引
self.initialSlide = opts.initialSlide || 0;
// 当前slide的索引
var _activeIndex = self.initialSlide;
function slideTo(slide) {
var ctx = self;
slide = slide % slideCount;
if (ctx.direction === 'horizontal') {
curPoint.x = nextPoint.x = originStart.x - slide * interval;
} else {
curPoint.y = nextPoint.y = originStart.y - slide * interval;
}
$(wrapper)
.addClass('transition');
translate(wrapper, nextPoint.x, nextPoint.y);
}
// 当前slide的索引,修改此变量会引起滑动
Object.defineProperty(self, 'activeIndex', {
get: function () {
return _activeIndex;
},
set: function (value) {
slideTo(value);
_activeIndex = value;
}
});
// 初始化结束的回调
var onInit = opts.onInit || function (swiper) {};
// 开始滑动的回调
var onSlideChangeStart = opts.onSlideChangeStart || function (swiper) {};
// 结束滑动的回调
var onSlideChangeEnd = opts.onSlideChangeEnd || function (swiper) {};
// 滑动过渡结束的回调
var onTransitionEnd = opts.onTransitionEnd || function (swiper) {};
// container 内容区的宽度,private
var containerWidth = parseFloat(getComputedStyle(self.el)
.width);
// container 内容区的高度,private
var containerHeight = parseFloat(getComputedStyle(self.el)
.height);
// wrapper dom对象,private
var wrapper = this.el.find('.swiper-wrapper')[0];
// 所有的slide元素数组,private
var slides = Array.prototype.slice.call(wrapper.find('.swiper-slide')) || [];
// slide的个数,private
var slideCount = self.slideCount = slides.length;
// 每个单元的元素大小,在方向的大小,算margin border padding,private
var interval = 0;
// wrapper在transform坐标系中起始临界点, private
var originStart = {
x: 0,
y: 0
};
// wrapper在transform坐标系中最终临界点,privite
var originEnd = {
x: 0,
y: 0
};
var curPoint = {
x: 0,
y: 0
};
var lastStaticPoint = {
x: 0,
y: 0
};
var curTouchPoint = {
x: 0,
y: 0
};
var startTouchPoint = {
x: 0,
y: 0
};
var lastTouchPoint = {
x: 0,
y: 0
};
var nextPoint = {
x: 0,
y: 0
};
var sensitivity = opts.sensitivity || 2;
function translate(elem, x, y) {
/* eslint no-func-assign: 0 */
if (elem.style.transform != undefined) {
translate = function (elem, x, y) {
elem.style.transform = 'translate3d(' + x + 'px, ' + y + 'px, 0px)';
};
} else {
translate = function (elem, x, y) {
elem.style.webkitTransform = 'translate3d(' + x + 'px, ' + y +
'px, 0px)';
};
}
translate(elem, x, y);
}
var operation = {
setHorizontalMargin: function (spaceBetween) {
slides.forEach(function (slide, idx, arr) {
if (idx === arr.length - 1) {
return;
}
slide.style.marginRight = spaceBetween + 'px';
});
},
setVerticalMargin: function (spaceBetween) {
slides.forEach(function (slide, idx, arr) {
if (idx === arr.length - 1) {
return;
}
slide.style.marginBottom = spaceBetween + 'px';
});
},
getBoxWidth: function (elem) {
var slideComputedStyle = getComputedStyle(elem);
var slideWidth = parseFloat(slideComputedStyle.width);
var slidePdLeft = parseFloat(slideComputedStyle.paddingLeft);
var slidePdRight = parseFloat(slideComputedStyle.paddingRight);
var slideBorderLeft = parseFloat(slideComputedStyle.borderLeftWidth);
var slideBorderRight = parseFloat(slideComputedStyle.borderRightWidth);
return slideWidth + slidePdLeft + slidePdRight + slideBorderLeft +
slideBorderRight;
},
getBoxHeight: function (elem) {
var slideComputedStyle = getComputedStyle(elem);
var slideHeight = parseFloat(slideComputedStyle.height);
var slidePdTop = parseFloat(slideComputedStyle.paddingTop);
var slidePdBottom = parseFloat(slideComputedStyle.paddingBottom);
var slideBorderTop = parseFloat(slideComputedStyle.borderTopWidth);
var slideBorderBottom = parseFloat(slideComputedStyle.borderBottomWidth);
return slideHeight + slidePdTop + slidePdBottom + slideBorderTop +
slideBorderBottom;
},
transformAxisX: function (slideBoxWidth) {
if (self.centeredSlides) {
// transform坐标系原点变换
originStart.x = (containerWidth - slideBoxWidth) / 2;
// transform坐标系变换,以originStart点为原点
curPoint.x = lastStaticPoint.x = nextPoint.x = originStart.x;
}
originEnd.x = originStart.x - (slideCount - 1) * interval;
curPoint.x -= self.initialSlide * interval;
lastStaticPoint.x = curPoint.x;
},
transformAxisY: function (slideBoxHeight) {
if (self.centeredSlides) {
// transform坐标系原点变换
originStart.y = (containerHeight - slideBoxHeight) / 2;
// transform坐标系变换,以originStart点为原点
curPoint.y = lastStaticPoint.y = nextPoint.y = originStart.y;
}
originEnd.y = originStart.y - (slideCount - 1) * interval;
curPoint.y -= self.initialSlide * interval;
lastStaticPoint.y = curPoint.y;
},
getMultiple: function (distance) {
var v = 0;
if (distance < 0) {
v = 1;
} else if (distance > 0) {
v = -1;
}
if (Math.abs(distance) % interval < interval / sensitivity) {
return Math.floor(Math.abs(distance) / interval) * v;
} else {
return Math.ceil(Math.abs(distance) / interval) * v;
}
}
};
function init(opts) {
var ctx = self;
if (ctx.direction === 'horizontal') {
if (ctx.spaceBetween) {
operation.setHorizontalMargin(ctx.spaceBetween);
}
var slideBoxWidth = operation.getBoxWidth(slides[0]);
interval = slideBoxWidth + ctx.spaceBetween;
operation.transformAxisX(slideBoxWidth);
translate(wrapper, curPoint.x, 0);
} else if (ctx.direction === 'vertical') {
wrapper.addClass('swiper-wrapper-vertical');
slides.forEach(function (slide) {
slide.addClass('swiper-slide-vertical');
});
if (ctx.spaceBetween) {
operation.setVerticalMargin(ctx.spaceBetween);
}
var slideBoxHeight = operation.getBoxHeight(slides[0]);
interval = slideBoxHeight + ctx.spaceBetween;
operation.transformAxisY(slideBoxHeight);
translate(wrapper, 0, curPoint.y);
}
onInit(self);
}
init();
$(wrapper)
.on('touchstart', function (event) {
lastTouchPoint.x = startTouchPoint.x = event.targetTouches[0].screenX;
lastTouchPoint.y = startTouchPoint.y = event.targetTouches[0].screenY;
lastStaticPoint.x = curPoint.x;
lastStaticPoint.y = curPoint.y;
onSlideChangeStart(self);
});
$(wrapper)
.on('touchmove', function (event) {
curTouchPoint.x = event.targetTouches[0].screenX;
curTouchPoint.y = event.targetTouches[0].screenY;
var dx = curTouchPoint.x - lastTouchPoint.x;
var dy = curTouchPoint.y - lastTouchPoint.y;
curPoint.x += dx;
curPoint.y += dy;
if (self.direction === 'horizontal') {
translate(wrapper, curPoint.x, 0);
} else {
translate(wrapper, 0, curPoint.y);
}
lastTouchPoint.x = curTouchPoint.x;
lastTouchPoint.y = curTouchPoint.y;
});
$(wrapper)
.on('touchend', function (event) {
// touchend的targetTouches是空的
curTouchPoint.x = event.changedTouches[0].screenX;
curTouchPoint.y = event.changedTouches[0].screenY;
// 本次滑动的距离
var distanceX = curTouchPoint.x - startTouchPoint.x;
var distanceY = curTouchPoint.y - startTouchPoint.y;
var multiple = 0;
var isStatic = false;
if (self.direction === 'horizontal') {
Math.abs(distanceX) < 1 && (isStatic = true);
multiple = operation.getMultiple(distanceX);
} else {
Math.abs(distanceX) < 1 && (isStatic = true);
multiple = operation.getMultiple(distanceY);
}
if (self.activeIndex + multiple < 0) {
self.activeIndex = 0;
} else if (self.activeIndex + multiple > slideCount - 1) {
self.activeIndex = slideCount - 1;
} else {
self.activeIndex += multiple;
}
if (isStatic) {
wrapper.removeClass('transition');
}
onSlideChangeEnd(self);
});
function transitionEnd(event) {
$(this)
.removeClass('transition');
onTransitionEnd(self);
}
$(wrapper)
.on('transitionend', transitionEnd);
$(wrapper)
.on('webkitTransitionEnd', transitionEnd);
$(wrapper)
.on('oTransitionEnd', transitionEnd);
return self;
}
return Swiper;
});