-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjetpack-carousel.js
69 lines (68 loc) · 2.28 KB
/
jetpack-carousel.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
jQuery(document).ready(function($) {
$('.gallery').modaal({
type: 'image'
});
});
// Swipe gesture detection
(function($) {
$.fn.touchwipe = function(settings) {
var config = {
min_move_x: 20,
min_move_y: 20,
wipeLeft: function() {},
wipeRight: function() {},
wipeUp: function() {},
wipeDown: function() {},
preventDefaultEvents: true
};
if (settings) $.extend(config, settings);
this.each(function() {
var startX;
var startY;
var isMoving = false;
function cancelTouch() {
this.removeEventListener('touchmove', onTouchMove);
startX = null;
isMoving = false;
}
function onTouchMove(e) {
if (config.preventDefaultEvents) {
e.preventDefault();
}
if (isMoving) {
var x = e.touches[0].pageX;
var y = e.touches[0].pageY;
var dx = startX - x;
var dy = startY - y;
if (Math.abs(dx) >= config.min_move_x) {
cancelTouch();
if (dx > 0) {
config.wipeLeft();
} else {
config.wipeRight();
}
} else if (Math.abs(dy) >= config.min_move_y) {
cancelTouch();
if (dy > 0) {
config.wipeDown();
} else {
config.wipeUp();
}
}
}
}
function onTouchStart(e) {
if (e.touches.length == 1) {
startX = e.touches[0].pageX;
startY = e.touches[0].pageY;
isMoving = true;
this.addEventListener('touchmove', onTouchMove, false);
}
}
if ('ontouchstart' in document.documentElement) {
this.addEventListener('touchstart', onTouchStart, false);
}
});
return this;
};
})(jQuery);