1 /*******************************************************************************
3 Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
4 email: info@pupunzi.com
5 site: http://pupunzi.com
8 http://www.opensource.org/licenses/mit-license.php
9 http://www.gnu.org/licenses/gpl.html
10 ******************************************************************************/
13 * hoverIntent is similar to jQuery's built-in "hover" function except that
14 * instead of firing the onMouseOver event immediately, hoverIntent checks
15 * to see if the user's mouse has slowed down (beneath the sensitivity
16 * threshold) before firing the onMouseOver event.
18 * hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
19 * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
21 * hoverIntent is currently available for use in all personal or commercial
22 * projects under both MIT and GPL licenses. This means that you can choose
23 * the license that best suits your project, and use it accordingly.
25 * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
26 * $("ul li").hoverIntent( showNav , hideNav );
28 * // advanced usage receives configuration object only
29 * $("ul li").hoverIntent({
30 * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
31 * interval: 100, // number = milliseconds of polling interval
32 * over: showNav, // function = onMouseOver callback (required)
33 * timeout: 0, // number = milliseconds delay before onMouseOut function call
34 * out: hideNav // function = onMouseOut callback (required)
37 * @param f onMouseOver function || An object with configuration options
38 * @param g onMouseOut function || Nothing (use configuration options object)
39 * @author Brian Cherne <brian@cherne.net>
42 $.fn.hoverIntent = function(f,g) {
43 // default configuration options
49 // override configuration options with user supplied object
50 cfg = $.extend(cfg, g ? { over: f, out: g } : f );
52 // instantiate variables
53 // cX, cY = current X and Y position of mouse, updated by mousemove event
54 // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
57 // A private function for getting mouse position
58 var track = function(ev) {
63 // A private function for comparing current and previous mouse position
64 var compare = function(ev,ob) {
65 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
66 // compare mouse positions to see if they've crossed the threshold
67 if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
68 $(ob).unbind("mousemove",track);
69 // set hoverIntent state to true (so mouseOut can be called)
71 return cfg.over.apply(ob,[ev]);
73 // set previous coordinates for next time
75 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
76 ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
80 // A private function for delaying the mouseOut function
81 var delay = function(ev,ob) {
82 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
84 return cfg.out.apply(ob,[ev]);
87 // A private function for handling mouse 'hovering'
88 var handleHover = function(e) {
89 // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
90 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
91 while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
92 if ( p == this ) { return false; }
94 // copy objects to be passed into t (required for event object to be passed in IE)
95 var ev = jQuery.extend({},e);
98 // cancel hoverIntent timer if it exists
99 if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
101 // else e.type == "onmouseover"
102 if (e.type == "mouseover") {
103 // set "previous" X and Y position based on initial entry point
104 pX = ev.pageX; pY = ev.pageY;
105 // update "current" X and Y position based on mousemove
106 $(ob).bind("mousemove",track);
107 // start polling interval (self-calling timeout) to compare mouse coordinates over time
108 if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
110 // else e.type == "onmouseout"
112 // unbind expensive mousemove event
113 $(ob).unbind("mousemove",track);
114 // if hoverIntent state is true, then call the mouseOut function after the specified delay
115 if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
119 // bind the function to the two event listeners
120 return this.mouseover(handleHover).mouseout(handleHover);