This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtappy.js
121 lines (102 loc) · 2.84 KB
/
tappy.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*! Tappy! - a lightweight normalized tap event. Copyright 2013 @scottjehl, Filament Group, Inc. Licensed MIT */
(function( w, $, undefined ){
// handling flag is true when an event sequence is in progress (thx androood)
w.tapHandling = false;
w.tappy = true;
var tap = function( $els ){
return $els.each(function(){
var $el = $( this ),
resetTimer,
startY,
startX,
cancel,
scrollTolerance = 10;
function trigger( e ){
$( e.target ).trigger( "tap", [ e, $( e.target ).attr( "href" ) ] );
}
function getCoords( e ){
var ev = e.originalEvent || e,
touches = ev.touches || ev.targetTouches;
if( touches ){
return [ touches[ 0 ].pageX, touches[ 0 ].pageY ];
}
else {
return null;
}
}
function start( e ){
if( e.touches && e.touches.length > 1 || e.targetTouches && e.targetTouches.length > 1 ){
return false;
}
var coords = getCoords( e );
startX = coords[ 0 ];
startY = coords[ 1 ];
}
// any touchscroll that results in > tolerance should cancel the tap
function move( e ){
if( !cancel ){
var coords = getCoords( e );
if( coords && ( Math.abs( startY - coords[ 1 ] ) > scrollTolerance || Math.abs( startX - coords[ 0 ] ) > scrollTolerance ) ){
cancel = true;
}
}
}
function end( e ){
clearTimeout( resetTimer );
resetTimer = setTimeout( function(){
w.tapHandling = false;
cancel = false;
}, 1000 );
// make sure no modifiers are present. thx http://www.jacklmoore.com/notes/click-events/
if( ( e.which && e.which > 1 ) || e.shiftKey || e.altKey || e.metaKey || e.ctrlKey ){
return;
}
e.preventDefault();
// this part prevents a double callback from touch and mouse on the same tap
// if a scroll happened between touchstart and touchend
if( cancel || w.tapHandling && w.tapHandling !== e.type ){
cancel = false;
return;
}
w.tapHandling = e.type;
trigger( e );
}
$el
.bind( "touchstart.tappy MSPointerDown.tappy", start )
.bind( "touchmove.tappy MSPointerMove.tappy", move )
.bind( "touchend.tappy MSPointerUp.tappy", end )
.bind( "click.tappy", end );
});
};
var untap = function( $els ){
return $els.unbind( ".tappy" );
};
// use special events api
if( $.event && $.event.special ){
$.event.special.tap = {
add: function( handleObj ) {
tap( $( this ) );
},
remove: function( handleObj ) {
untap( $( this ) );
}
};
}
else{
// monkeybind
var oldBind = $.fn.bind,
oldUnbind = $.fn.unbind;
$.fn.bind = function( evt ){
if( /(^| )tap( |$)/.test( evt ) ){
tap( this );
}
return oldBind.apply( this, arguments );
};
$.fn.unbind = function( evt ){
if( /(^| )tap( |$)/.test( evt ) ){
untap( this );
}
return oldUnbind.apply( this, arguments );
};
}
}( this, jQuery ));