/
jquery.hoverdelay.js
51 lines (37 loc) · 1.3 KB
/
jquery.hoverdelay.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
// jQuery Hover Delay 1.1.0 (20130401)
// By John Terenzio | http://terenz.io/ | MIT License
(function($) {
'use strict';
// original hover method
var oldHover = $.fn.hover,
// enhanced hover method
newHover = function(handlerIn, handlerOut, delay) {
return this.each(function() {
var timeout,
handler = function(el, fn, e) {
if (timeout) {
timeout = window.clearTimeout(timeout); // also set timeout to undefined
} else {
timeout = window.setTimeout(function() {
timeout = undefined;
fn.call(el, e);
}, delay);
}
};
$(this).on('mouseenter mouseleave', function(e) {
handler(this, e.type === 'mouseenter' ? handlerIn : handlerOut, e);
});
});
};
// call either original hover method or new hover method depending on
// whether a delay argurment was passed, can take one or two handlers
$.fn.hover = function(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
if (args.length === 3 && typeof args[2] === 'number') {
return newHover.apply(this, args);
} else if (args.length === 2 && typeof args[1] === 'number') {
return newHover.call(this, args[0], args[0], args[1]);
}
return oldHover.apply(this, args);
};
})(window.jQuery);