web/wp-includes/js/hoverIntent.dev.js
author ymh <ymh.work@gmail.com>
Mon, 22 Mar 2010 16:36:28 +0100
changeset 5 ac511f1ccc8e
parent 1 0d28b7c10758
permissions -rw-r--r--
add hgignore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
0d28b7c10758 First commit
ymh
parents:
diff changeset
     1
/**
0d28b7c10758 First commit
ymh
parents:
diff changeset
     2
* hoverIntent is similar to jQuery's built-in "hover" function except that
0d28b7c10758 First commit
ymh
parents:
diff changeset
     3
* instead of firing the onMouseOver event immediately, hoverIntent checks
0d28b7c10758 First commit
ymh
parents:
diff changeset
     4
* to see if the user's mouse has slowed down (beneath the sensitivity
0d28b7c10758 First commit
ymh
parents:
diff changeset
     5
* threshold) before firing the onMouseOver event.
0d28b7c10758 First commit
ymh
parents:
diff changeset
     6
* 
0d28b7c10758 First commit
ymh
parents:
diff changeset
     7
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
0d28b7c10758 First commit
ymh
parents:
diff changeset
     8
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
0d28b7c10758 First commit
ymh
parents:
diff changeset
     9
* 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    10
* hoverIntent is currently available for use in all personal or commercial 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    11
* projects under both MIT and GPL licenses. This means that you can choose 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    12
* the license that best suits your project, and use it accordingly.
0d28b7c10758 First commit
ymh
parents:
diff changeset
    13
* 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    14
* // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
0d28b7c10758 First commit
ymh
parents:
diff changeset
    15
* $("ul li").hoverIntent( showNav , hideNav );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    16
* 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    17
* // advanced usage receives configuration object only
0d28b7c10758 First commit
ymh
parents:
diff changeset
    18
* $("ul li").hoverIntent({
0d28b7c10758 First commit
ymh
parents:
diff changeset
    19
*	sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    20
*	interval: 100,   // number = milliseconds of polling interval
0d28b7c10758 First commit
ymh
parents:
diff changeset
    21
*	over: showNav,  // function = onMouseOver callback (required)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    22
*	timeout: 0,   // number = milliseconds delay before onMouseOut function call
0d28b7c10758 First commit
ymh
parents:
diff changeset
    23
*	out: hideNav    // function = onMouseOut callback (required)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    24
* });
0d28b7c10758 First commit
ymh
parents:
diff changeset
    25
* 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    26
* @param  f  onMouseOver function || An object with configuration options
0d28b7c10758 First commit
ymh
parents:
diff changeset
    27
* @param  g  onMouseOut function  || Nothing (use configuration options object)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    28
* @author    Brian Cherne <brian@cherne.net>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    29
*/
0d28b7c10758 First commit
ymh
parents:
diff changeset
    30
(function($) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    31
	$.fn.hoverIntent = function(f,g) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    32
		// default configuration options
0d28b7c10758 First commit
ymh
parents:
diff changeset
    33
		var cfg = {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    34
			sensitivity: 7,
0d28b7c10758 First commit
ymh
parents:
diff changeset
    35
			interval: 100,
0d28b7c10758 First commit
ymh
parents:
diff changeset
    36
			timeout: 0
0d28b7c10758 First commit
ymh
parents:
diff changeset
    37
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
    38
		// override configuration options with user supplied object
0d28b7c10758 First commit
ymh
parents:
diff changeset
    39
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    40
0d28b7c10758 First commit
ymh
parents:
diff changeset
    41
		// instantiate variables
0d28b7c10758 First commit
ymh
parents:
diff changeset
    42
		// cX, cY = current X and Y position of mouse, updated by mousemove event
0d28b7c10758 First commit
ymh
parents:
diff changeset
    43
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
0d28b7c10758 First commit
ymh
parents:
diff changeset
    44
		var cX, cY, pX, pY;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    45
0d28b7c10758 First commit
ymh
parents:
diff changeset
    46
		// A private function for getting mouse position
0d28b7c10758 First commit
ymh
parents:
diff changeset
    47
		var track = function(ev) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    48
			cX = ev.pageX;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    49
			cY = ev.pageY;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    50
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
    51
0d28b7c10758 First commit
ymh
parents:
diff changeset
    52
		// A private function for comparing current and previous mouse position
0d28b7c10758 First commit
ymh
parents:
diff changeset
    53
		var compare = function(ev,ob) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    54
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    55
			// compare mouse positions to see if they've crossed the threshold
0d28b7c10758 First commit
ymh
parents:
diff changeset
    56
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    57
				$(ob).unbind("mousemove",track);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    58
				// set hoverIntent state to true (so mouseOut can be called)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    59
				ob.hoverIntent_s = 1;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    60
				return cfg.over.apply(ob,[ev]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    61
			} else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    62
				// set previous coordinates for next time
0d28b7c10758 First commit
ymh
parents:
diff changeset
    63
				pX = cX; pY = cY;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    64
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
0d28b7c10758 First commit
ymh
parents:
diff changeset
    65
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    66
			}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    67
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
    68
0d28b7c10758 First commit
ymh
parents:
diff changeset
    69
		// A private function for delaying the mouseOut function
0d28b7c10758 First commit
ymh
parents:
diff changeset
    70
		var delay = function(ev,ob) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    71
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    72
			ob.hoverIntent_s = 0;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    73
			return cfg.out.apply(ob,[ev]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    74
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
    75
		
0d28b7c10758 First commit
ymh
parents:
diff changeset
    76
		// workaround for Mozilla bug: not firing mouseout/mouseleave on absolute positioned elements over textareas and input type="text"
0d28b7c10758 First commit
ymh
parents:
diff changeset
    77
		var handleHover = function(e) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    78
			var t = this;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    79
			
0d28b7c10758 First commit
ymh
parents:
diff changeset
    80
			// next two lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
0d28b7c10758 First commit
ymh
parents:
diff changeset
    81
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    82
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
0d28b7c10758 First commit
ymh
parents:
diff changeset
    83
			if ( p == this ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    84
				if ( $.browser.mozilla ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    85
					if ( e.type == "mouseout" ) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    86
						t.mtout = setTimeout( function(){doHover(e,t);}, 30 );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    87
					} else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    88
						if (t.mtout) { t.mtout = clearTimeout(t.mtout); }
0d28b7c10758 First commit
ymh
parents:
diff changeset
    89
					}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    90
				}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    91
				return;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    92
			} else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    93
				if (t.mtout) { t.mtout = clearTimeout(t.mtout); }
0d28b7c10758 First commit
ymh
parents:
diff changeset
    94
				doHover(e,t);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    95
			}
0d28b7c10758 First commit
ymh
parents:
diff changeset
    96
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
    97
0d28b7c10758 First commit
ymh
parents:
diff changeset
    98
		// A private function for handling mouse 'hovering'
0d28b7c10758 First commit
ymh
parents:
diff changeset
    99
		var doHover = function(e,ob) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   100
0d28b7c10758 First commit
ymh
parents:
diff changeset
   101
			// copy objects to be passed into t (required for event object to be passed in IE)
0d28b7c10758 First commit
ymh
parents:
diff changeset
   102
			var ev = jQuery.extend({},e);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   103
0d28b7c10758 First commit
ymh
parents:
diff changeset
   104
			// cancel hoverIntent timer if it exists
0d28b7c10758 First commit
ymh
parents:
diff changeset
   105
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
0d28b7c10758 First commit
ymh
parents:
diff changeset
   106
0d28b7c10758 First commit
ymh
parents:
diff changeset
   107
			// else e.type == "onmouseover"
0d28b7c10758 First commit
ymh
parents:
diff changeset
   108
			if (e.type == "mouseover") {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   109
				// set "previous" X and Y position based on initial entry point
0d28b7c10758 First commit
ymh
parents:
diff changeset
   110
				pX = ev.pageX; pY = ev.pageY;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   111
				// update "current" X and Y position based on mousemove
0d28b7c10758 First commit
ymh
parents:
diff changeset
   112
				$(ob).bind("mousemove",track);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   113
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
0d28b7c10758 First commit
ymh
parents:
diff changeset
   114
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   115
0d28b7c10758 First commit
ymh
parents:
diff changeset
   116
			// else e.type == "onmouseout"
0d28b7c10758 First commit
ymh
parents:
diff changeset
   117
			} else {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   118
				// unbind expensive mousemove event
0d28b7c10758 First commit
ymh
parents:
diff changeset
   119
				$(ob).unbind("mousemove",track);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   120
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
0d28b7c10758 First commit
ymh
parents:
diff changeset
   121
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   122
			}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   123
		};
0d28b7c10758 First commit
ymh
parents:
diff changeset
   124
0d28b7c10758 First commit
ymh
parents:
diff changeset
   125
		// bind the function to the two event listeners
0d28b7c10758 First commit
ymh
parents:
diff changeset
   126
		return this.mouseover(handleHover).mouseout(handleHover);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   127
	};
0d28b7c10758 First commit
ymh
parents:
diff changeset
   128
})(jQuery);