This repository has been archived by the owner on May 24, 2021. It is now read-only.
forked from component/tap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (71 loc) · 1.39 KB
/
index.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
/**
* Module Dependencies
*/
var event = require('event'),
bind = require('bind');
/**
* Expose `Tap`
*/
module.exports = Tap;
/**
* Touch support
*/
var support = 'ontouchstart' in window;
/**
* Tap on `el` to trigger a `fn`
*
* Tap will not fire if you move your finger
* to scroll
*
* @param {Element} el
* @param {Function} fn
*/
function Tap(el, fn) {
if(!(this instanceof Tap)) return new Tap(el, fn);
this.el = el;
this.fn = fn || function() {};
this.tap = true;
if (support) {
this.ontouchmove = bind(this, this.touchmove);
this.ontouchend = bind(this, this.touchend);
event.bind(el, 'touchmove', this.ontouchmove);
event.bind(el, 'touchend', this.ontouchend);
} else {
event.bind(el, 'click', this.fn);
}
}
/**
* Touch end
*
* @param {Event} e
* @return {Tap}
* @api private
*/
Tap.prototype.touchend = function(e) {
if (this.tap) this.fn(e);
this.tap = true;
event.bind(this.el, 'touchmove', this.ontouchmove);
return this;
};
/**
* Touch move
*
* @return {Tap}
* @api private
*/
Tap.prototype.touchmove = function() {
this.tap = false;
event.unbind(this.el, 'touchmove', this.ontouchmove);
return this;
};
/**
* Unbind the tap
*
* @return {Tap}
* @api public
*/
Tap.prototype.unbind = function() {
event.unbind(this.el, 'touchend', this.ontouchend);
event.unbind(this.el, 'click', this.fn);
return this;
};