You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
7.1 KiB

1 year ago
  1. /*!
  2. * hoverIntent v1.10.2 // 2020.04.28 // jQuery v1.7.0+
  3. * http://briancherne.github.io/jquery-hoverIntent/
  4. *
  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.
  7. * Copyright 2007-2019 Brian Cherne
  8. */
  9. /**
  10. * hoverIntent is similar to jQuery's built-in "hover" method except that
  11. * instead of firing the handlerIn function immediately, hoverIntent checks
  12. * to see if the user's mouse has slowed down (beneath the sensitivity
  13. * threshold) before firing the event. The handlerOut function is only
  14. * called after a matching handlerIn.
  15. *
  16. * // basic usage ... just like .hover()
  17. * .hoverIntent( handlerIn, handlerOut )
  18. * .hoverIntent( handlerInOut )
  19. *
  20. * // basic usage ... with event delegation!
  21. * .hoverIntent( handlerIn, handlerOut, selector )
  22. * .hoverIntent( handlerInOut, selector )
  23. *
  24. * // using a basic configuration object
  25. * .hoverIntent( config )
  26. *
  27. * @param handlerIn function OR configuration object
  28. * @param handlerOut function OR selector for delegation OR undefined
  29. * @param selector selector OR undefined
  30. * @author Brian Cherne <brian(at)cherne(dot)net>
  31. */
  32. ;(function(factory) {
  33. 'use strict';
  34. if (typeof define === 'function' && define.amd) {
  35. define(['jquery'], factory);
  36. } else if (typeof module === 'object' && module.exports) {
  37. module.exports = factory(require('jquery'));
  38. } else if (jQuery && !jQuery.fn.hoverIntent) {
  39. factory(jQuery);
  40. }
  41. })(function($) {
  42. 'use strict';
  43. // default configuration values
  44. var _cfg = {
  45. interval: 100,
  46. sensitivity: 6,
  47. timeout: 0
  48. };
  49. // counter used to generate an ID for each instance
  50. var INSTANCE_COUNT = 0;
  51. // current X and Y position of mouse, updated during mousemove tracking (shared across instances)
  52. var cX, cY;
  53. // saves the current pointer position coordinates based on the given mousemove event
  54. var track = function(ev) {
  55. cX = ev.pageX;
  56. cY = ev.pageY;
  57. };
  58. // compares current and previous mouse positions
  59. var compare = function(ev,$el,s,cfg) {
  60. // compare mouse positions to see if pointer has slowed enough to trigger `over` function
  61. if ( Math.sqrt( (s.pX-cX)*(s.pX-cX) + (s.pY-cY)*(s.pY-cY) ) < cfg.sensitivity ) {
  62. $el.off(s.event,track);
  63. delete s.timeoutId;
  64. // set hoverIntent state as active for this element (permits `out` handler to trigger)
  65. s.isActive = true;
  66. // overwrite old mouseenter event coordinates with most recent pointer position
  67. ev.pageX = cX; ev.pageY = cY;
  68. // clear coordinate data from state object
  69. delete s.pX; delete s.pY;
  70. return cfg.over.apply($el[0],[ev]);
  71. } else {
  72. // set previous coordinates for next comparison
  73. s.pX = cX; s.pY = cY;
  74. // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
  75. s.timeoutId = setTimeout( function(){compare(ev, $el, s, cfg);} , cfg.interval );
  76. }
  77. };
  78. // triggers given `out` function at configured `timeout` after a mouseleave and clears state
  79. var delay = function(ev,$el,s,out) {
  80. var data = $el.data('hoverIntent');
  81. if (data) {
  82. delete data[s.id];
  83. }
  84. return out.apply($el[0],[ev]);
  85. };
  86. // checks if `value` is a function
  87. var isFunction = function(value) {
  88. return typeof value === 'function';
  89. };
  90. $.fn.hoverIntent = function(handlerIn,handlerOut,selector) {
  91. // instance ID, used as a key to store and retrieve state information on an element
  92. var instanceId = INSTANCE_COUNT++;
  93. // extend the default configuration and parse parameters
  94. var cfg = $.extend({}, _cfg);
  95. if ( $.isPlainObject(handlerIn) ) {
  96. cfg = $.extend(cfg, handlerIn);
  97. if ( !isFunction(cfg.out) ) {
  98. cfg.out = cfg.over;
  99. }
  100. } else if ( isFunction(handlerOut) ) {
  101. cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
  102. } else {
  103. cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
  104. }
  105. // A private function for handling mouse 'hovering'
  106. var handleHover = function(e) {
  107. // cloned event to pass to handlers (copy required for event object to be passed in IE)
  108. var ev = $.extend({},e);
  109. // the current target of the mouse event, wrapped in a jQuery object
  110. var $el = $(this);
  111. // read hoverIntent data from element (or initialize if not present)
  112. var hoverIntentData = $el.data('hoverIntent');
  113. if (!hoverIntentData) { $el.data('hoverIntent', (hoverIntentData = {})); }
  114. // read per-instance state from element (or initialize if not present)
  115. var state = hoverIntentData[instanceId];
  116. if (!state) { hoverIntentData[instanceId] = state = { id: instanceId }; }
  117. // state properties:
  118. // id = instance ID, used to clean up data
  119. // timeoutId = timeout ID, reused for tracking mouse position and delaying "out" handler
  120. // isActive = plugin state, true after `over` is called just until `out` is called
  121. // pX, pY = previously-measured pointer coordinates, updated at each polling interval
  122. // event = string representing the namespaced event used for mouse tracking
  123. // clear any existing timeout
  124. if (state.timeoutId) { state.timeoutId = clearTimeout(state.timeoutId); }
  125. // namespaced event used to register and unregister mousemove tracking
  126. var mousemove = state.event = 'mousemove.hoverIntent.hoverIntent'+instanceId;
  127. // handle the event, based on its type
  128. if (e.type === 'mouseenter') {
  129. // do nothing if already active
  130. if (state.isActive) { return; }
  131. // set "previous" X and Y position based on initial entry point
  132. state.pX = ev.pageX; state.pY = ev.pageY;
  133. // update "current" X and Y position based on mousemove
  134. $el.off(mousemove,track).on(mousemove,track);
  135. // start polling interval (self-calling timeout) to compare mouse coordinates over time
  136. state.timeoutId = setTimeout( function(){compare(ev,$el,state,cfg);} , cfg.interval );
  137. } else { // "mouseleave"
  138. // do nothing if not already active
  139. if (!state.isActive) { return; }
  140. // unbind expensive mousemove event
  141. $el.off(mousemove,track);
  142. // if hoverIntent state is true, then call the mouseOut function after the specified delay
  143. state.timeoutId = setTimeout( function(){delay(ev,$el,state,cfg.out);} , cfg.timeout );
  144. }
  145. };
  146. // listen for mouseenter and mouseleave
  147. return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);
  148. };
  149. });