forked from thebird/Swipe
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswipe.js
497 lines (345 loc) · 12.8 KB
/
swipe.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Swipe 2.0
*
* Brad Birdsall
* Copyright 2012, Licensed GPL & MIT
*
*/
window.Swipe = function(element, options) {
var _this = this;
// return immediately if element doesn't exist
if (!element) return;
// reference dom elements
this.container = element;
this.element = this.container.children[0];
// simple feature detection
this.browser = {
touch: (function() {
return ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
})(),
transitions: (function() {
var temp = document.createElement('swipe'),
props = ['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective'];
for ( var i in props ) {
if (temp.style[ props[i] ] !== undefined) return true;
}
return false;
})()
};
// retreive options
options = options || {};
this.index = options.startSlide || 0;
this.speed = options.speed || 300;
this.callback = options.callback || function() {};
this.transitionEnd = options.transitionEnd || function() {};
this.delay = options.auto || 0;
this.cont = (options.continuous != undefined) ? !!options.continuous : true;
this.disableScroll = !!options.disableScroll;
// verify index is a number not string
this.index = parseInt(this.index,10);
// trigger slider initialization
this.setup();
// begin auto slideshow
this.begin();
// add event listeners
if (this.element.addEventListener) {
if (!!this.browser.touch) {
this.element.addEventListener('touchstart', this, false);
this.element.addEventListener('touchmove', this, false);
this.element.addEventListener('touchend', this, false);
}
if (!!this.browser.transitions) {
this.element.addEventListener('webkitTransitionEnd', this, false);
this.element.addEventListener('msTransitionEnd', this, false);
this.element.addEventListener('oTransitionEnd', this, false);
this.element.addEventListener('transitionend', this, false);
}
window.addEventListener('resize', this, false);
}
// to play nice with old IE
else {
window.onresize = function () {
_this.setup();
};
}
};
Swipe.prototype = {
setup: function() {
// get and measure amt of slides
this.slides = this.element.children;
this.length = this.slides.length;
this.cache = new Array(this.length);
// return immediately if there are no slides
if (this.length < 1) return;
// determine width of each slide
this.width = this.container.getBoundingClientRect().width || this.container.offsetWidth;
// return immediately if measurement fails
if (!this.width) return;
// store array of slides before, current, and after
var refArray = [[],[],[]];
this.element.style.width = (this.slides.length * this.width) + 'px';
// stack elements
for (var index = this.length - 1; index > -1; index--) {
var elem = this.slides[index];
elem.style.width = this.width + 'px';
elem.setAttribute('data-index', index);
if (this.browser.transitions) {
elem.style.left = (index * -this.width) + 'px';
}
// add this index to the reference array 0:before 1:equal 2:after
refArray[this.index > index ? 0 : (this.index < index ? 2 : 1)].push(index);
}
if (this.browser.transitions) {
// stack left, current, and right slides
this._stack(refArray[0],-1);
this._stack(refArray[1],0);
this._stack(refArray[2],1);
}
this.container.style.visibility = 'visible';
},
kill: function() {
// cancel slideshow
this.delay = 0;
clearTimeout(this.interval);
// clear all translations
var slideArray = [];
for (var i = this.slides.length - 1; i >= 0; i--) {
this.slides[i].style.width = '';
slideArray.push(i);
}
this._stack(slideArray,0);
var elem = this.element;
elem.className = elem.className.replace('swipe-active','');
// remove event listeners
if (this.element.removeEventListener) {
if (!!this.browser.touch) {
this.element.removeEventListener('touchstart', this, false);
this.element.removeEventListener('touchmove', this, false);
this.element.removeEventListener('touchend', this, false);
}
if (!!this.browser.transitions) {
this.element.removeEventListener('webkitTransitionEnd', this, false);
this.element.removeEventListener('msTransitionEnd', this, false);
this.element.removeEventListener('oTransitionEnd', this, false);
this.element.removeEventListener('transitionend', this, false);
}
window.removeEventListener('resize', this.resize, false);
}
// kill old IE! you can quote me on that ;)
else {
window.onresize = null;
}
},
getPos: function() {
// return current index position
return this.index;
},
prev: function(delay) {
// cancel slideshow
this.delay = delay || 0;
clearTimeout(this.interval);
// if not at first slide
if (this.index) this.slide(this.index-1, this.speed);
else if (this.cont) this.slide(this.length-1, this.speed);
},
next: function(delay) {
// cancel slideshow
this.delay = delay || 0;
clearTimeout(this.interval);
if (this.index < this.length - 1) this.slide(this.index+1, this.speed); // if not last slide
else if (this.cont) this.slide(0, this.speed); //if last slide return to start
},
begin: function() {
var _this = this;
this.interval = (this.delay)
? setTimeout(function() {
_this.next(_this.delay);
}, this.delay)
: 0;
},
handleEvent: function(e) {
switch (e.type) {
case 'touchstart': this.onTouchStart(e); break;
case 'touchmove': this.onTouchMove(e); break;
case 'touchend': this.onTouchEnd(e); break;
case 'webkitTransitionEnd':
case 'msTransitionEnd':
case 'oTransitionEnd':
case 'transitionend': this.onTransitionEnd(e); break;
case 'resize': this.setup(); break;
}
},
onTouchStart: function(e) {
var _this = this;
_this.start = {
// get touch coordinates for delta calculations in onTouchMove
pageX: e.touches[0].pageX,
pageY: e.touches[0].pageY,
// set initial timestamp of touch sequence
time: Number( new Date() )
};
// used for testing first onTouchMove event
_this.isScrolling = undefined;
// reset deltaX
_this.deltaX = 0;
},
onTouchMove: function(e) {
var _this = this;
// ensure swiping with one touch and not pinching
if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
_this.deltaX = e.touches[0].pageX - _this.start.pageX;
// determine if scrolling test has run - one time test
if ( typeof _this.isScrolling == 'undefined') {
_this.isScrolling = !!( _this.isScrolling || Math.abs(_this.deltaX) < Math.abs(e.touches[0].pageY - _this.start.pageY) );
}
// if user is not trying to scroll vertically
if (!_this.isScrolling) {
// prevent native scrolling
e.preventDefault();
// cancel slideshow
_this.delay = 0;
clearTimeout(_this.interval);
// increase resistance if first or last slide
_this.deltaX =
_this.deltaX /
( (!_this.index && _this.deltaX > 0 // if first slide and sliding left
|| _this.index == _this.length - 1 // or if last slide and sliding right
&& _this.deltaX < 0 // and if sliding at all
) ?
( Math.abs(_this.deltaX) / _this.width + 1 ) // determine resistance level
: 1 ); // no resistance if false
// translate immediately 1:1
_this._move([_this.index-1,_this.index,_this.index+1],_this.deltaX);
} else if (_this.disableScroll) {
// prevent native scrolling
e.preventDefault();
}
},
onTouchEnd: function(e) {
var _this = this;
// determine if slide attempt triggers next/prev slide
var isValidSlide =
Number(new Date()) - _this.start.time < 250 // if slide duration is less than 250ms
&& Math.abs(_this.deltaX) > 20 // and if slide amt is greater than 20px
|| Math.abs(_this.deltaX) > _this.width/2, // or if slide amt is greater than half the width
// determine if slide attempt is past start and end
isPastBounds =
!_this.index && _this.deltaX > 0 // if first slide and slide amt is greater than 0
|| _this.index == _this.length - 1 && _this.deltaX < 0, // or if last slide and slide amt is less than 0
direction = _this.deltaX < 0; // true:right false:left
// if not scrolling vertically
if (!_this.isScrolling) {
if (isValidSlide && !isPastBounds) {
if (direction) {
_this._stack([_this.index-1],-1);
_this._slide([_this.index,_this.index+1],-_this.width,_this.speed);
_this.index += 1;
} else {
_this._stack([_this.index+1],1);
_this._slide([_this.index-1,_this.index],_this.width,_this.speed);
_this.index += -1;
}
_this.callback(_this.index, _this.slides[_this.index]);
} else {
_this._slide([_this.index-1,_this.index,_this.index+1],0,_this.speed);
}
}
},
onTransitionEnd: function(e) {
if (this._getElemIndex(e.target) == this.index) { // only call transition end on the main slide item
if (this.delay) this.begin();
this.transitionEnd(this.index, this.slides[this.index]);
}
},
slide: function(to, speed) {
var from = this.index;
if (from == to) return; // do nothing if already on requested slide
if (this.browser.transitions) {
var toStack = Math.abs(from-to) - 1,
direction = Math.abs(from-to) / (from-to), // 1:right -1:left
inBetween = [];
while (toStack--) inBetween.push( (to > from ? to : from) - toStack - 1 );
// stack em
this._stack(inBetween,direction);
// now slide from and to in the proper direction
this._slide([from,to],this.width * direction,this.speed);
}
else {
this._animate(from*-this.width, to * -this.width, this.speed)
}
this.index = to;
this.callback(this.index, this.slides[this.index]);
},
_slide: function(nums, dist, speed) {
var _slides = this.slides,
l = nums.length;
while(l--) {
this._translate(_slides[nums[l]], dist + this.cache[nums[l]], speed ? speed : 0);
this.cache[nums[l]] += dist;
}
},
_stack: function(nums, pos) { // pos: -1:left 0:center 1:right
var _slides = this.slides,
l = nums.length,
dist = this.width * pos;
while(l--) {
this._translate(_slides[nums[l]], dist, 0);
this.cache[nums[l]] = dist;
}
},
_move: function(nums, dist) { // 1:1 scrolling
var _slides = this.slides,
l = nums.length;
while(l--) this._translate(_slides[nums[l]], dist + this.cache[nums[l]], 0);
},
_translate: function(elem, xval, speed) {
if (!elem) return;
var style = elem.style;
// set duration speed to 0
style.webkitTransitionDuration =
style.MozTransitionDuration =
style.msTransitionDuration =
style.OTransitionDuration =
style.transitionDuration = speed + 'ms';
// translate to given position
style.webkitTransform = 'translate3d(' + xval + 'px,0,0)';
style.msTransform =
style.MozTransform =
style.OTransform = 'translateX(' + xval + 'px)';
},
_animate: function(from, to, speed) {
var elem = this.element;
if (!speed) { // if not an animation, just reposition
elem.style.left = to + 'px';
return;
}
var _this = this,
start = new Date(),
timer = setInterval(function() {
var timeElap = new Date() - start;
if (timeElap > speed) {
elem.style.left = to + 'px'; // callback after this line
if (_this._getElemIndex(elem) == _this.index) { // only call transition end on the main slide item
if (_this.delay) _this.begin();
_this.transitionEnd(_this.index, _this.slides[_this.index]);
}
clearInterval(timer);
return;
}
elem.style.left = (( (to - from) * (Math.floor((timeElap / speed) * 100) / 100) ) + from) + 'px';
}, 4);
},
_getElemIndex: function(elem) {
return parseInt(elem.getAttribute('data-index'),10);
}
};
if ( window.jQuery || window.Zepto ) {
(function($) {
$.fn.Swipe = function(params) {
return this.each(function() {
var _this = $(this);
_this.data('Swipe', new Swipe(_this[0], params));
});
}
})( window.jQuery || window.Zepto )
}