forked from jonpacker/jquery.tap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tap.js
103 lines (93 loc) · 2.99 KB
/
jquery.tap.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
;var jqt = function($, undefined) {
var INVALIDATE_CLICKS_AFTER_TAP_THRESHOLD = 600;
var incrementalElementId = 0;
var mutex = 0;
$.fn.tap = function(threshold, callback, touchOnly) {
if (typeof threshold === 'function') {
touchOnly = callback;
callback = threshold;
threshold = 15;
}
if ('ontouchstart' in window) {
this.each(function() {
var moveDistance = 0;
var touch = null;
var elementId = ++incrementalElementId;
var startPoint = null
var touching = false;
var self = this;
var $self = $(this);
var invalidateClicksBefore = null;
$self.click(function() {
if (invalidateClicksBefore != null && Date.now() < invalidateClicksBefore) {
return;
} else {
callback.apply(self, arguments);
}
});
$self.bind('touchstart', function(e) {
if (mutex != 0) return;
else mutex = elementId;
touching = true;
moveDistance = 0;
if (e.originalEvent.touches && e.originalEvent.touches[0]) {
touch = e.originalEvent.touches[0];
startPoint = {
x: touch.screenX,
y: touch.screenY,
px: touch.pageX,
py: touch.pageY,
cx: touch.clientX,
cy: touch.clientY
}
}
});
$self.bind('touchend', function(e) {
if (mutex == elementId) mutex = 0;
if (!touching) return;
touching = false;
if (moveDistance < threshold) {
invalidateClicksBefore = Date.now() + INVALIDATE_CLICKS_AFTER_TAP_THRESHOLD;
e.pageX = startPoint.px;
e.pageY = startPoint.py;
e.clientX = startPoint.cx;
e.clientY = startPoint.cy;
e.screenX = startPoint.x;
e.scrrenY = startPoint.y;
callback.apply(self, arguments);
} else {
$self.trigger('tap-failed');
}
});
$self.bind('touchmove', function(e) {
if (!touching) return;
if (e.originalEvent.touches.length == 0 || startPoint === null) {
return touching = false;
}
touch = e.originalEvent.touches[0];
moveDistance = Math.sqrt(Math.pow(touch.screenX - startPoint.x, 2) +
Math.pow(touch.screenY - startPoint.y, 2));
if (moveDistance > threshold) {
$self.trigger('exceed-tap-threshold');
touching = false;
}
});
$self.bind('touchcancel', function() {
if (mutex == elementId) mutex = 0;
touching = false;
$self.trigger('tap-failed');
})
})
} else if (!touchOnly) {
this.click(callback);
}
return this;
}
};
if (module && module.exports) {
module.exports = function(jq) {
return jqt(jq || window.jQuery || window.$)
};
} else {
jqt(window.jQuery || window.$)
}