1 (function(a){a.fn.hoverIntent=function(k,j){var l={sensitivity:7,interval:100,timeout:0};l=a.extend(l,j?{over:k,out:j}:k);var n,m,h,d;var e=function(f){n=f.pageX;m=f.pageY};var c=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);if((Math.abs(h-n)+Math.abs(d-m))<l.sensitivity){a(f).unbind("mousemove",e);f.hoverIntent_s=1;return l.over.apply(f,[g])}else{h=n;d=m;f.hoverIntent_t=setTimeout(function(){c(g,f)},l.interval)}};var i=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);f.hoverIntent_s=0;return l.out.apply(f,[g])};var b=function(o){var g=jQuery.extend({},o);var f=this;if(f.hoverIntent_t){f.hoverIntent_t=clearTimeout(f.hoverIntent_t)}if(o.type=="mouseenter"){h=g.pageX;d=g.pageY;a(f).bind("mousemove",e);if(f.hoverIntent_s!=1){f.hoverIntent_t=setTimeout(function(){c(g,f)},l.interval)}}else{a(f).unbind("mousemove",e);if(f.hoverIntent_s==1){f.hoverIntent_t=setTimeout(function(){i(g,f)},l.timeout)}}};return this.bind("mouseenter",b).bind("mouseleave",b)}})(jQuery); |
1 /** |
|
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. |
|
6 * |
|
7 * hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+ |
|
8 * <http://cherne.net/brian/resources/jquery.hoverIntent.html> |
|
9 * |
|
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. |
|
13 * |
|
14 * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions |
|
15 * $("ul li").hoverIntent( showNav , hideNav ); |
|
16 * |
|
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) |
|
24 * }); |
|
25 * |
|
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(at)cherne(dot)net |
|
29 */ |
|
30 (function($) { |
|
31 $.fn.hoverIntent = function(f,g) { |
|
32 // default configuration options |
|
33 var cfg = { |
|
34 sensitivity: 7, |
|
35 interval: 100, |
|
36 timeout: 0 |
|
37 }; |
|
38 // override configuration options with user supplied object |
|
39 cfg = $.extend(cfg, g ? { over: f, out: g } : f ); |
|
40 |
|
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 |
|
44 var cX, cY, pX, pY; |
|
45 |
|
46 // A private function for getting mouse position |
|
47 var track = function(ev) { |
|
48 cX = ev.pageX; |
|
49 cY = ev.pageY; |
|
50 }; |
|
51 |
|
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) |
|
59 ob.hoverIntent_s = 1; |
|
60 return cfg.over.apply(ob,[ev]); |
|
61 } else { |
|
62 // set previous coordinates for next time |
|
63 pX = cX; pY = cY; |
|
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 ); |
|
66 } |
|
67 }; |
|
68 |
|
69 // A private function for delaying the mouseOut function |
|
70 var delay = function(ev,ob) { |
|
71 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
|
72 ob.hoverIntent_s = 0; |
|
73 return cfg.out.apply(ob,[ev]); |
|
74 }; |
|
75 |
|
76 // A private function for handling mouse 'hovering' |
|
77 var handleHover = function(e) { |
|
78 // copy objects to be passed into t (required for event object to be passed in IE) |
|
79 var ev = jQuery.extend({},e); |
|
80 var ob = this; |
|
81 |
|
82 // cancel hoverIntent timer if it exists |
|
83 if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } |
|
84 |
|
85 // if e.type == "mouseenter" |
|
86 if (e.type == "mouseenter") { |
|
87 // set "previous" X and Y position based on initial entry point |
|
88 pX = ev.pageX; pY = ev.pageY; |
|
89 // update "current" X and Y position based on mousemove |
|
90 $(ob).bind("mousemove",track); |
|
91 // start polling interval (self-calling timeout) to compare mouse coordinates over time |
|
92 if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );} |
|
93 |
|
94 // else e.type == "mouseleave" |
|
95 } else { |
|
96 // unbind expensive mousemove event |
|
97 $(ob).unbind("mousemove",track); |
|
98 // if hoverIntent state is true, then call the mouseOut function after the specified delay |
|
99 if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );} |
|
100 } |
|
101 }; |
|
102 |
|
103 // bind the function to the two event listeners |
|
104 return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover); |
|
105 }; |
|
106 })(jQuery); |