angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

hoverIntent.js (7099B)


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