1 | if (window.jQuery) { |
---|
2 | /** |
---|
3 | * hoverIntent is similar to jQuery's built-in "hover" function except that |
---|
4 | * instead of firing the onMouseOver event immediately, hoverIntent checks |
---|
5 | * to see if the user's mouse has slowed down (beneath the sensitivity |
---|
6 | * threshold) before firing the onMouseOver event. |
---|
7 | * |
---|
8 | * hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+ |
---|
9 | * <http://cherne.net/brian/resources/jquery.hoverIntent.html> |
---|
10 | * |
---|
11 | * hoverIntent is currently available for use in all personal or commercial |
---|
12 | * projects under both MIT and GPL licenses. This means that you can choose |
---|
13 | * the license that best suits your project, and use it accordingly. |
---|
14 | * |
---|
15 | * // basic usage (just like .hover) receives onMouseOver and onMouseOut functions |
---|
16 | * $("ul li").hoverIntent( showNav , hideNav ); |
---|
17 | * |
---|
18 | * // advanced usage receives configuration object only |
---|
19 | * $("ul li").hoverIntent({ |
---|
20 | * sensitivity: 7, // number = sensitivity threshold (must be 1 or higher) |
---|
21 | * interval: 100, // number = milliseconds of polling interval |
---|
22 | * over: showNav, // function = onMouseOver callback (required) |
---|
23 | * timeout: 0, // number = milliseconds delay before onMouseOut function call |
---|
24 | * out: hideNav // function = onMouseOut callback (required) |
---|
25 | * }); |
---|
26 | * |
---|
27 | * @param f onMouseOver function || An object with configuration options |
---|
28 | * @param g onMouseOut function || Nothing (use configuration options object) |
---|
29 | * @author Brian Cherne brian(at)cherne(dot)net |
---|
30 | */ |
---|
31 | (function($) { |
---|
32 | $.fn.hoverIntent = function(f,g) { |
---|
33 | // default configuration options |
---|
34 | var cfg = { |
---|
35 | sensitivity: 7, |
---|
36 | interval: 100, |
---|
37 | timeout: 0 |
---|
38 | }; |
---|
39 | // override configuration options with user supplied object |
---|
40 | cfg = $.extend(cfg, g ? { over: f, out: g } : f ); |
---|
41 | |
---|
42 | // instantiate variables |
---|
43 | // cX, cY = current X and Y position of mouse, updated by mousemove event |
---|
44 | // pX, pY = previous X and Y position of mouse, set by mouseover and polling interval |
---|
45 | var cX, cY, pX, pY; |
---|
46 | |
---|
47 | // A private function for getting mouse position |
---|
48 | var track = function(ev) { |
---|
49 | cX = ev.pageX; |
---|
50 | cY = ev.pageY; |
---|
51 | }; |
---|
52 | |
---|
53 | // A private function for comparing current and previous mouse position |
---|
54 | var compare = function(ev,ob) { |
---|
55 | ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
---|
56 | // compare mouse positions to see if they've crossed the threshold |
---|
57 | if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) { |
---|
58 | $(ob).unbind("mousemove",track); |
---|
59 | // set hoverIntent state to true (so mouseOut can be called) |
---|
60 | ob.hoverIntent_s = 1; |
---|
61 | return cfg.over.apply(ob,[ev]); |
---|
62 | } else { |
---|
63 | // set previous coordinates for next time |
---|
64 | pX = cX; pY = cY; |
---|
65 | // use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs) |
---|
66 | ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval ); |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | // A private function for delaying the mouseOut function |
---|
71 | var delay = function(ev,ob) { |
---|
72 | ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); |
---|
73 | ob.hoverIntent_s = 0; |
---|
74 | return cfg.out.apply(ob,[ev]); |
---|
75 | }; |
---|
76 | |
---|
77 | // A private function for handling mouse 'hovering' |
---|
78 | var handleHover = function(e) { |
---|
79 | // copy objects to be passed into t (required for event object to be passed in IE) |
---|
80 | var ev = jQuery.extend({},e); |
---|
81 | var ob = this; |
---|
82 | |
---|
83 | // cancel hoverIntent timer if it exists |
---|
84 | if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } |
---|
85 | |
---|
86 | // if e.type == "mouseenter" |
---|
87 | if (e.type == "mouseenter") { |
---|
88 | // set "previous" X and Y position based on initial entry point |
---|
89 | pX = ev.pageX; pY = ev.pageY; |
---|
90 | // update "current" X and Y position based on mousemove |
---|
91 | $(ob).bind("mousemove",track); |
---|
92 | // start polling interval (self-calling timeout) to compare mouse coordinates over time |
---|
93 | if (ob.hoverIntent_s != 1) { |
---|
94 | ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );} |
---|
95 | |
---|
96 | // else e.type == "mouseleave" |
---|
97 | } else { |
---|
98 | // unbind expensive mousemove event |
---|
99 | $(ob).unbind("mousemove",track); |
---|
100 | // if hoverIntent state is true, then call the mouseOut function after the specified delay |
---|
101 | if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );} |
---|
102 | } |
---|
103 | }; |
---|
104 | |
---|
105 | |
---|
106 | // bind the function to the two event listeners |
---|
107 | //radu return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover); |
---|
108 | return jQuery( this.context ).on( 'mouseenter mouseleave', this.selector, handleHover ); |
---|
109 | }; |
---|
110 | })(jQuery); |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | jQuery("li>a>img", jQuery("#thumbnails")).hoverIntent({ |
---|
127 | interval: 150, sensitivity: 3, |
---|
128 | over: function() { |
---|
129 | var $src_img = $(this), |
---|
130 | $pop = $("#pop") |
---|
131 | ,data = $src_img.data("pop"); |
---|
132 | if (!data) return; |
---|
133 | |
---|
134 | data = $.extend( { |
---|
135 | iw: $src_img.width(), |
---|
136 | ih: $src_img.height(), |
---|
137 | image: new Image |
---|
138 | }, data); |
---|
139 | data.image.src = data.url; |
---|
140 | if (window.devicePixelRatio) { |
---|
141 | data.w = Math.floor(data.w/window.devicePixelRatio); |
---|
142 | data.h = Math.floor(data.h/window.devicePixelRatio); |
---|
143 | } |
---|
144 | |
---|
145 | var finalLeft = $src_img.offset().left - (data.w-data.iw)/2, |
---|
146 | remaining = $(window).scrollLeft() + $(window).width() - (finalLeft+data.w) - 1 /*rounding*/; |
---|
147 | if (remaining<0) |
---|
148 | finalLeft+=remaining; |
---|
149 | finalLeft = Math.max(finalLeft, $(window).scrollLeft() ); |
---|
150 | |
---|
151 | var finalTop = $src_img.offset().top - (data.h-data.ih)/2, |
---|
152 | remaining = $(window).scrollTop() + $(window).height() - (finalTop+data.h) - 60 /*bottom description*/; |
---|
153 | if (remaining<0) |
---|
154 | finalTop+=remaining; |
---|
155 | finalTop = Math.max(finalTop, $(window).scrollTop() ); |
---|
156 | |
---|
157 | $pop.css( { |
---|
158 | left: $src_img.offset().left, |
---|
159 | top: $src_img.offset().top, |
---|
160 | width: data.iw |
---|
161 | }) |
---|
162 | .html('<div><a href="'+$src_img.parent('a').attr('href')+'"><img src="'+ $src_img.attr('src') + '" style="width:100%;height:'+data.ih+'px"></a></div>' |
---|
163 | +'<div style="overflow:hidden;height:5em;background-color:#000">'+$('.popDesc', $src_img.closest('li')).html()+'</div>') |
---|
164 | .show() |
---|
165 | .animate( { |
---|
166 | width: data.w+"px", |
---|
167 | left: finalLeft, |
---|
168 | top: finalTop |
---|
169 | },{ |
---|
170 | duration: 100, |
---|
171 | complete: function() { |
---|
172 | $pop.one("mouseleave", function() {$pop.hide()} ); |
---|
173 | }, |
---|
174 | step: function(tw,fx) { |
---|
175 | if (fx.prop !== "width") return; |
---|
176 | var th = tw * data.h / data.w; |
---|
177 | $("img", $pop).first().css("height", th); |
---|
178 | if (tw==data.iw) |
---|
179 | $("img", $pop).first().attr("src", data.url); |
---|
180 | } |
---|
181 | }) |
---|
182 | }, |
---|
183 | out: $.noop |
---|
184 | }); |
---|
185 | |
---|
186 | } //if (window.jQuery) |
---|