-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.tap.js
77 lines (67 loc) · 2.67 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
/**
* From https://github.com/jonpacker/jquery.tap
* The only difference is that the mapping to click events is deleted. Lines 71-73
*/
;(function($, undefined) {
'use strict';
var incrementalElementId = 0,
mutex = 0;
$.fn.tap = function(threshold, callback) {
if (typeof threshold === 'function') {
callback = threshold;
threshold = 15;
}
if ('ontouchstart' in window) {
this.each(function() {
var moveDistance = 0,
touch = null,
elementId = ++incrementalElementId,
startPoint = null,
touching = false,
self = this,
$self = $(this);
$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 };
}
});
$self.bind('touchend', function() {
if (mutex === elementId) {mutex = 0;}
if (!touching) {return;}
touching = false;
if (moveDistance < threshold) {
callback.apply(self, [].slice.call(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 {
this.click(callback);
}*/
return this;
};
})(window.jQuery || window.$);