hoverIntent.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ;(function($){
  2. /* hoverIntent by Brian Cherne */
  3. /*
  4. * PLEASE READ THE FOLLOWING BEFORE PLAYING AROUND WITH ANYTHING. KTHNX.
  5. * SMF Dev copy - Antechinus - 20th October 2011.
  6. * Code has been tweaked to give responsive menus without compromising a11y.
  7. * If contemplating changes, testing for full functionality is essential or a11y will be degraded.
  8. * Since a11y is the whole point of this system, degradation is not at all desirable regardless of personal preferences.
  9. * If you do not understand the a11y advantages of this system, please ask before making changes.
  10. *
  11. * Full functionality means:
  12. * 1/ hoverIntent plugin functions so that drop menus do NOT open or close instantly when cursor touches first level anchor.
  13. * 2/ The drop menus should only open when the cursor actually stops on the first level anchor, or is moving very slowly.
  14. * 3/ There should be a delay before the drop menus close on mouseout, for people with less than perfect tracking ability.
  15. * 4/ Settings for custom tooltips will be done separately in another file.
  16. */
  17. $.fn.hoverIntent = function(f,g) {
  18. // default configuration options
  19. var cfg = {
  20. sensitivity: 10,
  21. interval: 40,
  22. timeout: 1
  23. };
  24. // override configuration options with user supplied object
  25. cfg = $.extend(cfg, g ? { over: f, out: g } : f );
  26. // instantiate variables
  27. // cX, cY = current X and Y position of mouse, updated by mousemove event
  28. // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
  29. var cX, cY, pX, pY;
  30. // A private function for getting mouse position
  31. var track = function(ev) {
  32. cX = ev.pageX;
  33. cY = ev.pageY;
  34. };
  35. // A private function for comparing current and previous mouse position
  36. var compare = function(ev,ob) {
  37. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  38. // compare mouse positions to see if they've crossed the threshold
  39. if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
  40. $(ob).unbind("mousemove",track);
  41. // set hoverIntent state to true (so mouseOut can be called)
  42. ob.hoverIntent_s = 1;
  43. return cfg.over.apply(ob,[ev]);
  44. } else {
  45. // set previous coordinates for next time
  46. pX = cX; pY = cY;
  47. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  48. ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
  49. }
  50. };
  51. // A private function for delaying the mouseOut function
  52. var delay = function(ev,ob) {
  53. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  54. ob.hoverIntent_s = 0;
  55. return cfg.out.apply(ob,[ev]);
  56. };
  57. // A private function for handling mouse 'hovering'
  58. var handleHover = function(e) {
  59. // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
  60. var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
  61. while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
  62. if ( p == this ) { return false; }
  63. // copy objects to be passed into t (required for event object to be passed in IE)
  64. var ev = jQuery.extend({},e);
  65. var ob = this;
  66. // cancel hoverIntent timer if it exists
  67. if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
  68. // else e.type == "onmouseover"
  69. if (e.type == "mouseover") {
  70. // set "previous" X and Y position based on initial entry point
  71. pX = ev.pageX; pY = ev.pageY;
  72. // update "current" X and Y position based on mousemove
  73. $(ob).bind("mousemove",track);
  74. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  75. if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
  76. // else e.type == "onmouseout"
  77. } else {
  78. // unbind expensive mousemove event
  79. $(ob).unbind("mousemove",track);
  80. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  81. if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
  82. }
  83. };
  84. // bind the function to the two event listeners
  85. return this.mouseover(handleHover).mouseout(handleHover);
  86. };
  87. })(jQuery);