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