-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathjquery.doubletap.js
executable file
·130 lines (113 loc) · 4.01 KB
/
jquery.doubletap.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
/*
Copyright (c) 2010-* rick olson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function($) {
var touchStatus = function(target, touch) {
this.target = $(target);
this.touch = touch;
this.startX = this.currentX = touch.screenX;
this.startY = this.currentY = touch.screenY;
this.eventType = null;
}
touchStatus.latestTap = null;
touchStatus.prototype.move = function(touch) {
this.currentX = touch.screenX;
this.currentY = touch.screenY;
}
touchStatus.prototype.process = function() {
var offsetX = this.currentX - this.startX;
var offsetY = this.currentY - this.startY;
if(offsetX == 0 && offsetY == 0) {
this.checkForDoubleTap()
} else if(Math.abs(offsetY) > Math.abs(offsetX)) {
this.eventType = offsetY > 0 ? 'swipedown' : 'swipeup';
this.target.trigger('swipe', [this])
} else {
this.eventType = offsetX > 0 ? 'swiperight' : 'swipeleft';
this.target.trigger('swipe', [this])
}
this.target.trigger(this.eventType, [this])
this.target.trigger('touch', [this])
}
touchStatus.prototype.checkForDoubleTap = function() {
if(touchStatus.latestTap) {
if((new Date() - touchStatus.latestTap) < 400)
this.eventType = 'doubletap'
}
if(!this.eventType) this.eventType = 'tap'
touchStatus.latestTap = new Date()
}
var swipeEvents = function(elements) {
elements.bind('touchstart', this.touchStart);
elements.bind('touchmove', this.touchMove);
elements.bind('touchcancel', this.touchCancel);
elements.bind('touchend', this.touchEnd);
}
swipeEvents.prototype.touchStart = function(evt) {
var target = this;
swipeEvents.eachTouch(evt, function(touch) {
swipeEvents.touches[touch.identifier] = new touchStatus(target, touch);
})
}
swipeEvents.prototype.touchMove = function(evt) {
swipeEvents.eachTouch(evt, function(touch) {
var loc = swipeEvents.touches[touch.identifier]
if(loc) loc.move(touch)
})
}
swipeEvents.prototype.touchCancel = function(evt) {
swipeEvents.eachTouch(evt, function(touch) {
swipeEvents.purge(touch, true)
})
}
swipeEvents.prototype.touchEnd = function(evt) {
swipeEvents.eachTouch(evt, function(touch) {
swipeEvents.purge(touch)
})
}
swipeEvents.touches = {}
swipeEvents.purge = function(touch, cancelled) {
if(!cancelled) {
var loc = swipeEvents.touches[touch.identifier]
if(loc) loc.process()
}
delete swipeEvents.touches[touch.identifier]
}
swipeEvents.eachTouch = function(evt, callback) {
var evt = evt.originalEvent;
var num = evt.changedTouches.length;
for(var i = 0; i < num; i++) {
callback(evt.changedTouches[i])
}
}
// adds custom events:
// touch // all events
// swipe // only swipe* events
// swipeleft
// swiperight
// swipeup
// swipedown
// tap
// doubletap
$.fn.addSwipeEvents = function(callback) {
new swipeEvents(this);
if(callback) this.bind('touch', callback)
return this;
}
})(jQuery);