2 * hoverIntent is similar to jQuery's built-in "hover" function except that
3 * instead of firing the onMouseOver event immediately, hoverIntent checks
4 * to see if the user's mouse has slowed down (beneath the sensitivity
5 * threshold) before firing the onMouseOver event.
7 * hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
8 * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
10 * hoverIntent is currently available for use in all personal or commercial
11 * projects under both MIT and GPL licenses. This means that you can choose
12 * the license that best suits your project, and use it accordingly.
14 * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
15 * $("ul li").hoverIntent( showNav , hideNav );
17 * // advanced usage receives configuration object only
18 * $("ul li").hoverIntent({
19 * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
20 * interval: 100, // number = milliseconds of polling interval
21 * over: showNav, // function = onMouseOver callback (required)
22 * timeout: 0, // number = milliseconds delay before onMouseOut function call
23 * out: hideNav // function = onMouseOut callback (required)
26 * @param f onMouseOver function || An object with configuration options
27 * @param g onMouseOut function || Nothing (use configuration options object)
28 * @author Brian Cherne <brian@cherne.net>
31 $.fn
.hoverIntent = function(f
,g
) {
32 // default configuration options
38 // override configuration options with user supplied object
39 cfg
= $.extend(cfg
, g
? { over
: f
, out
: g
} : f
);
41 // instantiate variables
42 // cX, cY = current X and Y position of mouse, updated by mousemove event
43 // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
46 // A private function for getting mouse position
47 var track = function(ev
) {
52 // A private function for comparing current and previous mouse position
53 var compare = function(ev
,ob
) {
54 ob
.hoverIntent_t
= clearTimeout(ob
.hoverIntent_t
);
55 // compare mouse positions to see if they've crossed the threshold
56 if ( ( Math
.abs(pX
-cX
) + Math
.abs(pY
-cY
) ) < cfg
.sensitivity
) {
57 $(ob
).unbind("mousemove",track
);
58 // set hoverIntent state to true (so mouseOut can be called)
60 return cfg
.over
.apply(ob
,[ev
]);
62 // set previous coordinates for next time
64 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
65 ob
.hoverIntent_t
= setTimeout( function(){compare(ev
, ob
);} , cfg
.interval
);
69 // A private function for delaying the mouseOut function
70 var delay = function(ev
,ob
) {
71 ob
.hoverIntent_t
= clearTimeout(ob
.hoverIntent_t
);
73 return cfg
.out
.apply(ob
,[ev
]);
76 // A private function for handling mouse 'hovering'
77 var handleHover = function(e
) {
78 // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
79 var p
= (e
.type
== "mouseover" ? e
.fromElement
: e
.toElement
) || e
.relatedTarget
;
80 while ( p
&& p
!= this ) { try { p
= p
.parentNode
; } catch(e
) { p
= this; } }
81 if ( p
== this ) { return false; }
83 // copy objects to be passed into t (required for event object to be passed in IE)
84 var ev
= $.extend({},e
);
87 // cancel hoverIntent timer if it exists
88 if (ob
.hoverIntent_t
) { ob
.hoverIntent_t
= clearTimeout(ob
.hoverIntent_t
); }
90 // else e.type == "onmouseover"
91 if (e
.type
== "mouseover") {
92 // set "previous" X and Y position based on initial entry point
93 pX
= ev
.pageX
; pY
= ev
.pageY
;
94 // update "current" X and Y position based on mousemove
95 $(ob
).bind("mousemove",track
);
96 // start polling interval (self-calling timeout) to compare mouse coordinates over time
97 if (ob
.hoverIntent_s
!= 1) { ob
.hoverIntent_t
= setTimeout( function(){compare(ev
,ob
);} , cfg
.interval
);}
99 // else e.type == "onmouseout"
101 // unbind expensive mousemove event
102 $(ob
).unbind("mousemove",track
);
103 // if hoverIntent state is true, then call the mouseOut function after the specified delay
104 if (ob
.hoverIntent_s
== 1) { ob
.hoverIntent_t
= setTimeout( function(){delay(ev
,ob
);} , cfg
.timeout
);}
108 // bind the function to the two event listeners
109 return this.mouseover(handleHover
).mouseout(handleHover
);