1 /*! |
1 /*! |
2 * hoverIntent r7 // 2013.03.11 // jQuery 1.9.1+ |
2 * hoverIntent v1.8.1 // 2014.08.11 // jQuery v1.9.1+ |
3 * http://cherne.net/brian/resources/jquery.hoverIntent.html |
3 * http://cherne.net/brian/resources/jquery.hoverIntent.html |
4 * |
4 * |
5 * You may use hoverIntent under the terms of the MIT license. Basically that |
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. |
6 * means you are free to use hoverIntent as long as this header is left intact. |
7 * Copyright 2007, 2013 Brian Cherne |
7 * Copyright 2007, 2014 Brian Cherne |
8 */ |
8 */ |
9 |
9 |
10 /* hoverIntent is similar to jQuery's built-in "hover" method except that |
10 /* hoverIntent is similar to jQuery's built-in "hover" method except that |
11 * instead of firing the handlerIn function immediately, hoverIntent checks |
11 * instead of firing the handlerIn function immediately, hoverIntent checks |
12 * to see if the user's mouse has slowed down (beneath the sensitivity |
12 * to see if the user's mouse has slowed down (beneath the sensitivity |
60 |
60 |
61 // A private function for comparing current and previous mouse position |
61 // A private function for comparing current and previous mouse position |
62 var compare = function(ev,ob) { |
62 var compare = function(ev,ob) { |
63 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
63 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
64 // compare mouse positions to see if they've crossed the threshold |
64 // compare mouse positions to see if they've crossed the threshold |
65 if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) { |
65 if ( Math.sqrt( (pX-cX)*(pX-cX) + (pY-cY)*(pY-cY) ) < cfg.sensitivity ) { |
66 $(ob).off("mousemove.hoverIntent",track); |
66 $(ob).off("mousemove.hoverIntent",track); |
67 // set hoverIntent state to true (so mouseOut can be called) |
67 // set hoverIntent state to true (so mouseOut can be called) |
68 ob.hoverIntent_s = 1; |
68 ob.hoverIntent_s = true; |
69 return cfg.over.apply(ob,[ev]); |
69 return cfg.over.apply(ob,[ev]); |
70 } else { |
70 } else { |
71 // set previous coordinates for next time |
71 // set previous coordinates for next time |
72 pX = cX; pY = cY; |
72 pX = cX; pY = cY; |
73 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs) |
73 // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs) |
76 }; |
76 }; |
77 |
77 |
78 // A private function for delaying the mouseOut function |
78 // A private function for delaying the mouseOut function |
79 var delay = function(ev,ob) { |
79 var delay = function(ev,ob) { |
80 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
80 ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
81 ob.hoverIntent_s = 0; |
81 ob.hoverIntent_s = false; |
82 return cfg.out.apply(ob,[ev]); |
82 return cfg.out.apply(ob,[ev]); |
83 }; |
83 }; |
84 |
84 |
85 // A private function for handling mouse 'hovering' |
85 // A private function for handling mouse 'hovering' |
86 var handleHover = function(e) { |
86 var handleHover = function(e) { |
87 // copy objects to be passed into t (required for event object to be passed in IE) |
87 // copy objects to be passed into t (required for event object to be passed in IE) |
88 var ev = jQuery.extend({},e); |
88 var ev = $.extend({},e); |
89 var ob = this; |
89 var ob = this; |
90 |
90 |
91 // cancel hoverIntent timer if it exists |
91 // cancel hoverIntent timer if it exists |
92 if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } |
92 if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } |
93 |
93 |
94 // if e.type == "mouseenter" |
94 // if e.type === "mouseenter" |
95 if (e.type == "mouseenter") { |
95 if (e.type === "mouseenter") { |
96 // set "previous" X and Y position based on initial entry point |
96 // set "previous" X and Y position based on initial entry point |
97 pX = ev.pageX; pY = ev.pageY; |
97 pX = ev.pageX; pY = ev.pageY; |
98 // update "current" X and Y position based on mousemove |
98 // update "current" X and Y position based on mousemove |
99 $(ob).on("mousemove.hoverIntent",track); |
99 $(ob).on("mousemove.hoverIntent",track); |
100 // start polling interval (self-calling timeout) to compare mouse coordinates over time |
100 // start polling interval (self-calling timeout) to compare mouse coordinates over time |
101 if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );} |
101 if (!ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );} |
102 |
102 |
103 // else e.type == "mouseleave" |
103 // else e.type == "mouseleave" |
104 } else { |
104 } else { |
105 // unbind expensive mousemove event |
105 // unbind expensive mousemove event |
106 $(ob).off("mousemove.hoverIntent",track); |
106 $(ob).off("mousemove.hoverIntent",track); |
107 // if hoverIntent state is true, then call the mouseOut function after the specified delay |
107 // if hoverIntent state is true, then call the mouseOut function after the specified delay |
108 if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );} |
108 if (ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );} |
109 } |
109 } |
110 }; |
110 }; |
111 |
111 |
112 // listen for mouseenter and mouseleave |
112 // listen for mouseenter and mouseleave |
113 return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector); |
113 return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector); |