root / drupal7 / sites / all / themes / danland / scripts / hoverIntent.js @ c22e192e
1 | 85ad3d82 | Assos Assos | // $Id: hoverIntent.js,v 1.1 2010/07/19 22:25:16 danprobo Exp $
|
---|---|---|---|
2 | (function($){ |
||
3 | /* hoverIntent by Brian Cherne */
|
||
4 | $.fn.hoverIntent = function(f,g) { |
||
5 | // default configuration options
|
||
6 | var cfg = {
|
||
7 | sensitivity: 7, |
||
8 | interval: 100, |
||
9 | timeout: 0 |
||
10 | }; |
||
11 | // override configuration options with user supplied object
|
||
12 | cfg = $.extend(cfg, g ? { over: f, out: g } : f ); |
||
13 | |||
14 | // instantiate variables
|
||
15 | // cX, cY = current X and Y position of mouse, updated by mousemove event
|
||
16 | // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
|
||
17 | var cX, cY, pX, pY;
|
||
18 | |||
19 | // A private function for getting mouse position
|
||
20 | var track = function(ev) { |
||
21 | cX = ev.pageX; |
||
22 | cY = ev.pageY; |
||
23 | }; |
||
24 | |||
25 | // A private function for comparing current and previous mouse position
|
||
26 | var compare = function(ev,ob) { |
||
27 | ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
||
28 | // compare mouse positions to see if they've crossed the threshold
|
||
29 | if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
|
||
30 | $(ob).unbind("mousemove",track); |
||
31 | // set hoverIntent state to true (so mouseOut can be called)
|
||
32 | ob.hoverIntent_s = 1;
|
||
33 | return cfg.over.apply(ob,[ev]);
|
||
34 | } else {
|
||
35 | // set previous coordinates for next time
|
||
36 | pX = cX; pY = cY; |
||
37 | // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
|
||
38 | ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
|
||
39 | } |
||
40 | }; |
||
41 | |||
42 | // A private function for delaying the mouseOut function
|
||
43 | var delay = function(ev,ob) { |
||
44 | ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
||
45 | ob.hoverIntent_s = 0;
|
||
46 | return cfg.out.apply(ob,[ev]);
|
||
47 | }; |
||
48 | |||
49 | // A private function for handling mouse 'hovering'
|
||
50 | var handleHover = function(e) { |
||
51 | // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
|
||
52 | var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; |
||
53 | while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } } |
||
54 | if ( p == this ) { return false; } |
||
55 | |||
56 | // copy objects to be passed into t (required for event object to be passed in IE)
|
||
57 | var ev = jQuery.extend({},e);
|
||
58 | var ob = this; |
||
59 | |||
60 | // cancel hoverIntent timer if it exists
|
||
61 | if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
|
||
62 | |||
63 | // else e.type == "onmouseover"
|
||
64 | if (e.type == "mouseover") { |
||
65 | // set "previous" X and Y position based on initial entry point
|
||
66 | pX = ev.pageX; pY = ev.pageY; |
||
67 | // update "current" X and Y position based on mousemove
|
||
68 | $(ob).bind("mousemove",track); |
||
69 | // start polling interval (self-calling timeout) to compare mouse coordinates over time
|
||
70 | if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );} |
||
71 | |||
72 | // else e.type == "onmouseout"
|
||
73 | } else {
|
||
74 | // unbind expensive mousemove event
|
||
75 | $(ob).unbind("mousemove",track); |
||
76 | // if hoverIntent state is true, then call the mouseOut function after the specified delay
|
||
77 | if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );} |
||
78 | } |
||
79 | }; |
||
80 | |||
81 | // bind the function to the two event listeners
|
||
82 | return this.mouseover(handleHover).mouseout(handleHover); |
||
83 | }; |
||
84 | |||
85 | })(jQuery); |