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