PageSourceSearch

http://llmotorsport.se/resources/scripts/jquery.hoverIntent.js

js llmotorsport.se collected 2026-09-30 21:12:45 UTC 6,875 bytes, 153 lines download raw bytes

vendor: 6,875 bytes, lines 1-153
1/*!
2 * hoverIntent v1.8.1 // 2014.08.11 // jQuery v1.9.1+
3 * http://cherne.net/brian/resources/jquery.hoverIntent.html
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, 2014 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
33(function(factory) {
34    'use strict';
35    if (typeof define === 'function' && define.amd) {
36        define(['jquery'], factory);
37    } else if (jQuery && !jQuery.fn.hoverIntent) {
38        factory(jQuery);
39    }
40})(function($) {
41    'use strict';
42
43    // default configuration values
44    var _cfg = {
45        interval: 100,
46        sensitivity: 6,
47        timeout: 0
48    };
49
50    // counter used to generate an ID for each instance
51    var INSTANCE_COUNT = 0;
52
53    // current X and Y position of mouse, updated during mousemove tracking (shared across instances)
54    var cX, cY;
55
56    // saves the current pointer position coordinated based on the given mouse event
57    var track = function(ev) {
58        cX = ev.pageX;
59        cY = ev.pageY;
60    };
61
62    // compares current and previous mouse positions
63    var compare = function(ev,$el,s,cfg) {
64        // compare mouse positions to see if pointer has slowed enough to trigger `over` function
65        if ( Math.sqrt( (s.pX-cX)*(s.pX-cX) + (s.pY-cY)*(s.pY-cY) ) < cfg.sensitivity ) {
66            $el.off('mousemove.hoverIntent'+s.namespace,track);
67            delete s.timeoutId;
68            // set hoverIntent state as active for this element (so `out` handler can eventually be called)
69            s.isActive = true;
70            // clear coordinate data
71            delete s.pX; delete s.pY;
72            return cfg.over.apply($el[0],[ev]);
73        } else {
74            // set previous coordinates for next comparison
75            s.pX = cX; s.pY = cY;
76            // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
77            s.timeoutId = setTimeout( function(){compare(ev, $el, s, cfg);} , cfg.interval );
78        }
79    };
80
81    // triggers given `out` function at configured `timeout` after a mouseleave and clears state
82    var delay = function(ev,$el,s,out) {
83        delete $el.data('hoverIntent')[s.id];
84        return out.apply($el[0],[ev]);
85    };
86
87    $.fn.hoverIntent = function(handlerIn,handlerOut,selector) {
88        // instance ID, used as a key to store and retrieve state information on an element
89        var instanceId = INSTANCE_COUNT++;
90
91        // extend the default configuration and parse parameters
92        var cfg = $.extend({}, _cfg);
93        if ( $.isPlainObject(handlerIn) ) {
94            cfg = $.extend(cfg, handlerIn );
95        } else if ($.isFunction(handlerOut)) {
96            cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
97        } else {
98            cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
99        }
100
101        // A private function for handling mouse 'hovering'
102        var handleHover = function(e) {
103            // cloned event to pass to handlers (copy required for event object to be passed in IE)
104            var ev = $.extend({},e);
105
106            // the current target of the mouse event, wrapped in a jQuery object
107            var $el = $(this);
108
109            // read hoverIntent data from element (or initialize if not present)
110            var hoverIntentData = $el.data('hoverIntent');
111            if (!hoverIntentData) { $el.data('hoverIntent', (hoverIntentData = {})); }
112
113            // read per-instance state from element (or initialize if not present)
114            var state = hoverIntentData[instanceId];
115            if (!state) { hoverIntentData[instanceId] = state = { id: instanceId }; }
116
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            // namespace = string used as namespace for per-instance event management
123
124            // clear any existing timeout
125            if (state.timeoutId) { state.timeoutId = clearTimeout(state.timeoutId); }
126
127            // event namespace, used to register and unregister mousemove tracking
128            var namespace = state.namespace = '.hoverIntent'+instanceId;
129
130            // handle the event, based on its type
131            if (e.type === 'mouseenter') {
132                // do nothing if already active
133                if (state.isActive) { return; }
134                // set "previous" X and Y position based on initial entry point
135                state.pX = ev.pageX; state.pY = ev.pageY;
136                // update "current" X and Y position based on mousemove
137                $el.on('mousemove.hoverIntent'+namespace,track);
138                // start polling interval (self-calling timeout) to compare mouse coordinates over time
139                state.timeoutId = setTimeout( function(){compare(ev,$el,state,cfg);} , cfg.interval );
140            } else { // "mouseleave"
141                // do nothing if not already active
142                if (!state.isActive) { return; }
143                // unbind expensive mousemove event
144                $el.off('mousemove.hoverIntent'+namespace,track);
145                // if hoverIntent state is true, then call the mouseOut function after the specified delay
146                state.timeoutId = setTimeout( function(){delay(ev,$el,state,cfg.out);} , cfg.timeout );
147            }
148        };
149
150        // listen for mouseenter and mouseleave
151        return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);
152    };
153});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.