wp/wp-includes/js/hoverIntent.js
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 18:28:13 +0200
changeset 9 177826044cd9
parent 5 5e2f62d02dcd
child 18 be944660c56a
permissions -rw-r--r--
upgrade wordpress to 5.2.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/*!
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     2
 * hoverIntent v1.8.3 // 2014.08.11 // jQuery v1.9.1+
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * http://cherne.net/brian/resources/jquery.hoverIntent.html
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * You may use hoverIntent under the terms of the MIT license. Basically that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * means you are free to use hoverIntent as long as this header is left intact.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
 * Copyright 2007, 2014 Brian Cherne
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     9
 
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
/* hoverIntent is similar to jQuery's built-in "hover" method except that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * instead of firing the handlerIn function immediately, hoverIntent checks
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * to see if the user's mouse has slowed down (beneath the sensitivity
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * threshold) before firing the event. The handlerOut function is only
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * called after a matching handlerIn.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * // basic usage ... just like .hover()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * .hoverIntent( handlerIn, handlerOut )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * .hoverIntent( handlerInOut )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * // basic usage ... with event delegation!
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * .hoverIntent( handlerIn, handlerOut, selector )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * .hoverIntent( handlerInOut, selector )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * // using a basic configuration object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * .hoverIntent( config )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * @param  handlerIn   function OR configuration object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @param  handlerOut  function OR selector for delegation OR undefined
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 * @param  selector    selector OR undefined
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
 * @author Brian Cherne <brian(at)cherne(dot)net>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
(function($) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    $.fn.hoverIntent = function(handlerIn,handlerOut,selector) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        // default configuration values
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        var cfg = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            interval: 100,
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
            sensitivity: 6,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            timeout: 0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        if ( typeof handlerIn === "object" ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            cfg = $.extend(cfg, handlerIn );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        } else if ($.isFunction(handlerOut)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            cfg = $.extend(cfg, { over: handlerIn, out: handlerOut, selector: selector } );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            cfg = $.extend(cfg, { over: handlerIn, out: handlerIn, selector: handlerOut } );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        // instantiate variables
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        // cX, cY = current X and Y position of mouse, updated by mousemove event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        var cX, cY, pX, pY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        // A private function for getting mouse position
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        var track = function(ev) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            cX = ev.pageX;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            cY = ev.pageY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        // A private function for comparing current and previous mouse position
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        var compare = function(ev,ob) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            // compare mouse positions to see if they've crossed the threshold
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
            if ( Math.sqrt( (pX-cX)*(pX-cX) + (pY-cY)*(pY-cY) ) < cfg.sensitivity ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
                $(ob).off("mousemove.hoverIntent",track);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
                // set hoverIntent state to true (so mouseOut can be called)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
                ob.hoverIntent_s = true;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                return cfg.over.apply(ob,[ev]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
                // set previous coordinates for next time
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                pX = cX; pY = cY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
                // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        // A private function for delaying the mouseOut function
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        var delay = function(ev,ob) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
            ob.hoverIntent_s = false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            return cfg.out.apply(ob,[ev]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        // A private function for handling mouse 'hovering'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        var handleHover = function(e) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            // copy objects to be passed into t (required for event object to be passed in IE)
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
            var ev = $.extend({},e);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
            var ob = this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
            // cancel hoverIntent timer if it exists
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
            // if e.type === "mouseenter"
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
            if (e.type === "mouseenter") {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                // set "previous" X and Y position based on initial entry point
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
                pX = ev.pageX; pY = ev.pageY;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
                // update "current" X and Y position based on mousemove
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
                $(ob).on("mousemove.hoverIntent",track);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                // start polling interval (self-calling timeout) to compare mouse coordinates over time
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
                if (!ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                // else e.type == "mouseleave"
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            } else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
                // unbind expensive mousemove event
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                $(ob).off("mousemove.hoverIntent",track);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                // if hoverIntent state is true, then call the mouseOut function after the specified delay
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   108
                if (ob.hoverIntent_s) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
            }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        // listen for mouseenter and mouseleave
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        return this.on({'mouseenter.hoverIntent':handleHover,'mouseleave.hoverIntent':handleHover}, cfg.selector);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
})(jQuery);