jquery.hoverIntent.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * hoverIntent is similar to jQuery's built-in "hover" function except that
  3. * instead of firing the onMouseOver event immediately, hoverIntent checks
  4. * to see if the user's mouse has slowed down (beneath the sensitivity
  5. * threshold) before firing the onMouseOver event.
  6. *
  7. * hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
  8. * <http://cherne.net/brian/resources/jquery.hoverIntent.html>
  9. *
  10. * hoverIntent is currently available for use in all personal or commercial
  11. * projects under both MIT and GPL licenses. This means that you can choose
  12. * the license that best suits your project, and use it accordingly.
  13. *
  14. * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
  15. * $("ul li").hoverIntent( showNav , hideNav );
  16. *
  17. * // advanced usage receives configuration object only
  18. * $("ul li").hoverIntent({
  19. * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
  20. * interval: 100, // number = milliseconds of polling interval
  21. * over: showNav, // function = onMouseOver callback (required)
  22. * timeout: 0, // number = milliseconds delay before onMouseOut function call
  23. * out: hideNav // function = onMouseOut callback (required)
  24. * });
  25. *
  26. * @param f onMouseOver function || An object with configuration options
  27. * @param g onMouseOut function || Nothing (use configuration options object)
  28. * @author Brian Cherne brian(at)cherne(dot)net
  29. */
  30. (function ($) {
  31. $.fn.hoverIntent = function (f, g) {
  32. // default configuration options
  33. var cfg = {
  34. sensitivity:7,
  35. interval:100,
  36. timeout:0
  37. };
  38. // override configuration options with user supplied object
  39. cfg = $.extend(cfg, g ? { over:f, out:g } : f);
  40. // instantiate variables
  41. // cX, cY = current X and Y position of mouse, updated by mousemove event
  42. // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
  43. var cX, cY, pX, pY;
  44. // A private function for getting mouse position
  45. var track = function (ev) {
  46. cX = ev.pageX;
  47. cY = ev.pageY;
  48. };
  49. // A private function for comparing current and previous mouse position
  50. var compare = function (ev, ob) {
  51. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  52. // compare mouse positions to see if they've crossed the threshold
  53. if (( Math.abs(pX - cX) + Math.abs(pY - cY) ) < cfg.sensitivity) {
  54. $(ob).unbind("mousemove", track);
  55. // set hoverIntent state to true (so mouseOut can be called)
  56. ob.hoverIntent_s = 1;
  57. return cfg.over.apply(ob, [ev]);
  58. } else {
  59. // set previous coordinates for next time
  60. pX = cX;
  61. pY = cY;
  62. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  63. ob.hoverIntent_t = setTimeout(function () {
  64. compare(ev, ob);
  65. }, cfg.interval);
  66. }
  67. };
  68. // A private function for delaying the mouseOut function
  69. var delay = function (ev, ob) {
  70. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  71. ob.hoverIntent_s = 0;
  72. return cfg.out.apply(ob, [ev]);
  73. };
  74. // A private function for handling mouse 'hovering'
  75. var handleHover = function (e) {
  76. // copy objects to be passed into t (required for event object to be passed in IE)
  77. var ev = jQuery.extend({}, e);
  78. var ob = this;
  79. // cancel hoverIntent timer if it exists
  80. if (ob.hoverIntent_t) {
  81. ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
  82. }
  83. // if e.type == "mouseenter"
  84. if (e.type == "mouseenter") {
  85. // set "previous" X and Y position based on initial entry point
  86. pX = ev.pageX;
  87. pY = ev.pageY;
  88. // update "current" X and Y position based on mousemove
  89. $(ob).bind("mousemove", track);
  90. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  91. if (ob.hoverIntent_s != 1) {
  92. ob.hoverIntent_t = setTimeout(function () {
  93. compare(ev, ob);
  94. }, cfg.interval);
  95. }
  96. // else e.type == "mouseleave"
  97. } else {
  98. // unbind expensive mousemove event
  99. $(ob).unbind("mousemove", track);
  100. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  101. if (ob.hoverIntent_s == 1) {
  102. ob.hoverIntent_t = setTimeout(function () {
  103. delay(ev, ob);
  104. }, cfg.timeout);
  105. }
  106. }
  107. };
  108. // bind the function to the two event listeners
  109. return this.bind('mouseenter', handleHover).bind('mouseleave', handleHover);
  110. };
  111. })(jQuery);