-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.click-hold.js
90 lines (82 loc) · 2.52 KB
/
jquery.click-hold.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
/*
* jQuery click-and-hold plugin 1.0.1
* https://github.com/phuong/jqueryClickAndHold
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*/
;(function ($, window, document, undefined) {
var pluginName = "clickAndHold",
defaults = {
timeout: 200,
onHold: function (event, times) {
// Fire on hold element
},
onRelease: function (event) {
// Fire once element is released
}
};
function Plugin(element, options) {
if ($.isFunction(options)) {
options = {
onHold: options
}
}
this.$element = $(element);
this.mouseStillDown = false;
this.options = $.extend({}, defaults, options);
this.interval = null;
this.indicator = 0;
this.init();
}
var p = Plugin.prototype;
p.init = function () {
var self = this;
this.$element.mousedown(function (e) {
// Cache state and call onHold
self.mouseStillDown = true;
self.element = this;
self.event = e;
self.initialTimeout = self.options.timeout;
self.callAndCheck();
}).mouseup(function (e) {
// Restore state
self.mouseStillDown = false;
self.options.onRelease.call(this, e);
clearInterval(self.interval);
self.options.timeout = self.initialTimeout;
self.indicator = 0;
});
};
/**
* Call action and check mouse status
* @param element
* @param event
*/
p.callAndCheck = function (element, event) {
var self = this;
var options = self.options;
if (!self.mouseStillDown) {
return;
}
self.indicator++;
options.onHold.call(self.element, self.event, self.indicator);
if (self.mouseStillDown) {
// Call onHold faster and faster
if (options.timeout > 10) {
options.timeout -= 10;
}
clearInterval(self.interval);
self.interval = setInterval(function () {
self.callAndCheck();
}, options.timeout);
}
};
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);