forked from puppetlabs/showoff
/
jquery.doubletap-0.1.js
105 lines (94 loc) · 2.97 KB
/
jquery.doubletap-0.1.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
(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);