-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.doubletap.js
More file actions
executable file
·84 lines (69 loc) · 2.62 KB
/
jquery.doubletap.js
File metadata and controls
executable file
·84 lines (69 loc) · 2.62 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
76
77
78
79
80
81
82
83
84
// created by David Kane - 17/10/2012
// based on blog post that I saw here: http://stackoverflow.com/questions/5507638/whats-the-best-way-to-handle-longtap-and-double-tap-events-on-mobile-devices-us
(function($)
{
$.fn.doubletap = function(fn)
{
return fn ? this.bind('doubletap', fn) : this.trigger('doubletap');
};
$.attrFn.doubletap = true;
$.event.special.doubletap =
{
setup: function(data, namespaces)
{
$(this).bind('touchstart', $.event.special.doubletap.handler);
$(this).bind('touchmove', $.event.special.doubletap.handler);
$(this).bind('touchend', $.event.special.doubletap.handler);
},
teardown: function(namespaces)
{
$(this).unbind('touchstart', $.event.special.doubletap.handler);
$(this).unbind('touchmove', $.event.special.doubletap.handler);
$(this).unbind('touchend', $.event.special.doubletap.handler);
},
handler: function(event)
{
switch(event.type)
{
case 'touchstart' :
$(this).data('isScrolling', false);
return;
case 'touchmove' :
$(this).data('isScrolling', true);
return;
default :
break;
}
if($(this).data('isScrolling'))
return;
var action;
clearTimeout(action);
var now = new Date().getTime();
//the first time this will make delta a negative number
var lastTouch = $(this).data('lastTouch') || now + 1;
var delta = now - lastTouch;
var delay = delay == null? 500 : delay;
if(delta < delay && delta > 0)
{
// After we detct a doubletap, start over
$(this).data('lastTouch', null);
// set event type to 'doubletap'
event.type = 'doubletap';
// let jQuery handle the triggering of "doubletap" event handlers
$.event.handle.apply(this, arguments);
}
else
{
$(this).data('lastTouch', now);
action = setTimeout(function(evt)
{
// set event type to 'doubletap'
event.type = 'tap';
// let jQuery handle the triggering of "doubletap" event handlers
$.event.handle.apply(this, arguments);
clearTimeout(action); // clear the timeout
}, delay, [event]);
}
}
};
})(jQuery);