-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.swipe.js
More file actions
75 lines (70 loc) · 1.74 KB
/
jquery.swipe.js
File metadata and controls
75 lines (70 loc) · 1.74 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
(function($) {
var type = 'swipe',
ts = 'touchstart',
tm = 'touchmove',
te = 'touchend',
tc = 'touchcancel',
start = {x: 0, y: 0},
end = {x: 0, y: 0},
tolerance = 10,
touchStart = function(e) {
if (isTouchEvent(e)) {
start = getPos(e);
end = getPos(e);
}
},
touchMove = function(e) {
if (isTouchEvent(e)) {
end = getPos(e);
if (Math.abs(start.y - end.y) < tolerance) {
e.preventDefault();
}
}
},
touchEnd = function(e) {
var args = [].slice.call(arguments, 1),
event = $.event.fix(e);
event.type = type;
event.dx = end.x - start.x;
event.dy = end.y - start.y;
touchCancel();
if (Math.abs(event.dx) > tolerance || Math.abs(event.dy) > tolerance) {
args.unshift(event);
($.event.dispatch || $.event.handle).apply(this, args);
}
},
touchCancel = function(e) {
start.x = start.y = end.x = end.y = 0;
},
isTouchEvent = function(e) {
return e.targetTouches && e.targetTouches.length === 1;
},
getPos = function(e) {
var touch = e.targetTouches[0];
return {
x: touch.pageX,
y: touch.pageY
};
};
$.event.special[type] = {
setup: function() {
if (this.addEventListener) {
this.addEventListener(ts, touchStart, false);
this.addEventListener(tm, touchMove, false);
this.addEventListener(te, touchEnd, false);
this.addEventListener(tc, touchCancel, false);
}
},
teardown: function() {
if (this.removeEventListener) {
this.removeEventListener(ts, touchStart);
this.removeEventListener(tm, touchMove);
this.removeEventListener(te, touchEnd);
this.removeEventListener(tc, touchCancel);
}
}
};
$.fn[type] = function(fn) {
return fn ? this.bind(type, fn) : this.trigger(type);
};
})(jQuery);