2 * hoverIntent v1.8.1 // 2014.08.11 // jQuery v1.9.1+
3 * http://cherne.net/brian/resources/jquery.hoverIntent.html
5 * You may use hoverIntent under the terms of the MIT license. Basically that
6 * means you are free to use hoverIntent as long as this header is left intact.
7 * Copyright 2007, 2014 Brian Cherne
10 /* hoverIntent is similar to jQuery's built-in "hover" method except that
11 * instead of firing the handlerIn function immediately, hoverIntent checks
12 * to see if the user's mouse has slowed down (beneath the sensitivity
13 * threshold) before firing the event. The handlerOut function is only
14 * called after a matching handlerIn.
16 * // basic usage ... just like .hover()
17 * .hoverIntent( handlerIn, handlerOut )
18 * .hoverIntent( handlerInOut )
20 * // basic usage ... with event delegation!
21 * .hoverIntent( handlerIn, handlerOut, selector )
22 * .hoverIntent( handlerInOut, selector )
24 * // using a basic configuration object
25 * .hoverIntent( config )
27 * @param handlerIn function OR configuration object
28 * @param handlerOut function OR selector for delegation OR undefined
29 * @param selector selector OR undefined
30 * @author Brian Cherne <brian(at)cherne(dot)net>
33 $.fn.hoverIntent = function(handlerIn,handlerOut,selector) {
35 // default configuration values
42 if ( typeof handlerIn === "object" ) {
43 cfg = $.extend(cfg, handlerIn );
44 } else if ($.isFunction(handlerOut)) {
45 cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
47 cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
50 // instantiate variables
51 // cX, cY = current X and Y position of mouse, updated by mousemove event
52 // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
55 // A private function for getting mouse position
56 var track = function(ev) {
61 // A private function for comparing current and previous mouse position
62 var compare = function(ev,ob) {
63 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
64 // compare mouse positions to see if they've crossed the threshold
65 if ( Math.sqrt( (pX-cX)*(pX-cX) + (pY-cY)*(pY-cY) ) < cfg.sensitivity ) {
66 $(ob).off("mousemove.hoverIntent",track);
67 // set hoverIntent state to true (so mouseOut can be called)
68 ob.hoverIntent_s = true;
69 return cfg.over.apply(ob,[ev]);
71 // set previous coordinates for next time
73 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
74 ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
78 // A private function for delaying the mouseOut function
79 var delay = function(ev,ob) {
80 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
81 ob.hoverIntent_s = false;
82 return cfg.out.apply(ob,[ev]);
85 // A private function for handling mouse 'hovering'
86 var handleHover = function(e) {
87 // copy objects to be passed into t (required for event object to be passed in IE)
88 var ev = $.extend({},e);
91 // cancel hoverIntent timer if it exists
92 if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
94 // if e.type === "mouseenter"
95 if (e.type === "mouseenter") {
96 // set "previous" X and Y position based on initial entry point
97 pX = ev.pageX; pY = ev.pageY;
98 // update "current" X and Y position based on mousemove
99 $(ob).on("mousemove.hoverIntent",track);
100 // start polling interval (self-calling timeout) to compare mouse coordinates over time
101 if (!ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
103 // else e.type == "mouseleave"
105 // unbind expensive mousemove event
106 $(ob).off("mousemove.hoverIntent",track);
107 // if hoverIntent state is true, then call the mouseOut function after the specified delay
108 if (ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
112 // listen for mouseenter and mouseleave
113 return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);