-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.hoverDelay.js
More file actions
89 lines (68 loc) · 1.81 KB
/
jquery.hoverDelay.js
File metadata and controls
89 lines (68 loc) · 1.81 KB
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
/*
Hover Delay
Written by Jeff Mehlhoff, July 2010
jeffmehlhoff@me.com
*/
(function($) {
$.fn.hoverDelay = function(opts) {
// base config
var config = {
delay : 700,
initialize : function() { },
enter : function() { },
done : function() { },
out : function() { },
delayedOut : function() { }
}
$.extend(config, opts);
var obj = $(this);
var delayTimeout; var outTimeout;
var pX; var pY;
/* ---------- Helper methods */
$.cancelHoverDelay = function() {
clearTimeout(delayTimeout);
clearTimeout(outTimeout);
}
function unbindMouseMove() {
obj.unbind('mousemove', mouseMove);
}
function clearDifferenceTimeout() {
clearTimeout(delayTimeout);
}
/* ---------- Binding methods */
var difference = function(e) {
clearDifferenceTimeout();
delayTimeout = setTimeout(function(){
if((e.pageX === pX) && (e.pageY === pY)) {
unbindMouseMove();
config.done(e);
}
}, config.delay);
}
var mouseMove = function(e) {
pX = e.pageX;
pY = e.pageY;
difference(e);
}
var mouseEnter = function(e) {
clearTimeout(outTimeout);
obj.bind('mousemove', mouseMove);
config.enter(e);
}
var mouseOut = function(e) {
clearDifferenceTimeout();
unbindMouseMove();
outTimeout = setTimeout(function(){
config.delayedOut();
}, config.delay);
config.out(e);
}
/* ---------- Core functions */
function init() {
obj.live('mouseenter', mouseEnter);
obj.live('mouseout', mouseOut);
config.initialize();
}
init(); // get the party started
}
})(jQuery);