source: main/trunk/model-interfaces-dev/mars/iframe/support/hoverIntent-r7.js@ 34363

Last change on this file since 34363 was 34363, checked in by davidb, 4 years ago

Initial set of files for interface

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