forked from maaaaark/bcSwipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.bcSwipe.js
57 lines (51 loc) · 1.37 KB
/
jquery.bcSwipe.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
/**
* Bootstrap Carousel Swipe v1.1
*
* jQuery plugin to enable swipe gestures on Bootstrap 3 carousels.
* Examples and documentation: https://github.com/maaaaark/bcSwipe
*
* Licensed under the MIT license.
*/
(function($) {
$.fn.bcSwipe = function(settings) {
var config = { threshold: 50 };
if (settings) {
$.extend(config, settings);
}
this.each(function() {
var stillMoving = false;
var start;
if ('ontouchstart' in document.documentElement) {
this.addEventListener('touchstart', onTouchStart, false);
}
function onTouchStart(e) {
if (e.touches.length == 1) {
start = e.touches[0].pageX;
stillMoving = true;
this.addEventListener('touchmove', onTouchMove, false);
}
}
function onTouchMove(e) {
if (stillMoving) {
var x = e.touches[0].pageX;
var difference = start - x;
if (Math.abs(difference) >= config.threshold) {
cancelTouch();
if (difference > 0) {
$(this).carousel('next');
}
else {
$(this).carousel('prev');
}
}
}
}
function cancelTouch() {
this.removeEventListener('touchmove', onTouchMove);
start = null;
stillMoving = false;
}
});
return this;
};
})(jQuery);