-
Notifications
You must be signed in to change notification settings - Fork 7
/
jquery.touch.js
138 lines (110 loc) · 3.35 KB
/
jquery.touch.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*!
tap / double tap special event for jQuery
v 1.0.0
(c) 2014 Yair Even Or <http://dropthebit.com>
MIT-style license.
*/
;(function($){
"use strict";
var tapTimer,
moved = false, // flag to know if the finger had moved while touched the device
threshold = 250; // ms
//////////////////////
// special events
$.event.special.doubleTap = {
setup : setup,
teardown : teardown,
handler : handler
};
$.event.special.tap = {
setup : setup,
teardown : teardown,
handler : handler
};
//////////////////////
// events methods
function setup(data, namespaces){
var elm = $(this);
if( elm.data('tap_event') == true )
return;
elm.bind('touchend.tap', handler)
.bind('touchmove.tap', function(){
moved = true;
}).data('tap_event', true);
}
function teardown(namespaces) {
$(this).unbind('touchend.tap touchmove.tap');
}
function handler(event){
console.log(event);
if( moved ){ // reset
moved = false;
return false;
}
var elem = event.target,
$elem = $(elem),
lastTouch = $elem.data('lastTouch') || 0,
now = event.timeStamp,
delta = now - lastTouch;
// double-tap condition
if( delta > 20 && delta < threshold ){
clearTimeout(tapTimer);
return $elem.data('lastTouch', 0).trigger('doubleTap');
}
else
$elem.data('lastTouch', now);
tapTimer = setTimeout(function(){
$elem.trigger('tap');
}, threshold);
}
})(jQuery);
/**
* jQuery Plugin to add basic "swipe" support on touch-enabled devices
*
* @author Yair Even Or
* @version 1.0.0 (March 20, 2013)
*/
(function($){
"use strict";
$.event.special.swipe = {
setup: function(){
$(this).bind('touchstart', $.event.special.swipe.handler);
},
teardown: function(){
$(this).unbind('touchstart', $.event.special.swipe.handler);
},
handler: function(event){
var args = [].slice.call( arguments, 1 ), // clone arguments array, remove original event from cloned array
touches = event.originalEvent.touches,
startX, startY,
deltaX = 0, deltaY = 0,
that = this;
event = $.event.fix(event);
if( touches.length == 1 ){
startX = touches[0].pageX;
startY = touches[0].pageY;
this.addEventListener('touchmove', onTouchMove, false);
}
function cancelTouch(){
that.removeEventListener('touchmove', onTouchMove);
startX = startY = null;
}
function onTouchMove(e){
//e.preventDefault();
var Dx = startX - e.touches[0].pageX,
Dy = startY - e.touches[0].pageY;
if( Math.abs(Dx) >= 50 ){
cancelTouch();
deltaX = (Dx > 0) ? -1 : 1;
}
else if( Math.abs(Dy) >= 20 ){
cancelTouch();
deltaY = (Dy > 0) ? 1 : -1;
}
event.type = 'swipe';
args.unshift(event, deltaX, deltaY); // add back the new event to the front of the arguments with the delatas
return ($.event.dispatch || $.event.handle).apply(that, args);
}
}
};
})(jQuery);