Fixed getting styles from disconnected nodes. Fixes #7148.
[jquery.git] / src / effects.js
1 (function( jQuery ) {
2
3 var elemdisplay = {},
4         rfxtypes = /^(?:toggle|show|hide)$/,
5         rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/,
6         timerId,
7         fxAttrs = [
8                 // height animations
9                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
10                 // width animations
11                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
12                 // opacity animations
13                 [ "opacity" ]
14         ];
15
16 jQuery.fn.extend({
17         show: function( speed, easing, callback ) {
18                 if ( speed || speed === 0 ) {
19                         return this.animate( genFx("show", 3), speed, easing, callback);
20                 } else {
21                         for ( var i = 0, j = this.length; i < j; i++ ) {
22                                 // Reset the inline display of this element to learn if it is
23                                 // being hidden by cascaded rules or not
24                                 if ( !jQuery.data(this[i], "olddisplay") && this[i].style.display === "none" ) {
25                                         this[i].style.display = "";
26                                 }
27
28                                 // Set elements which have been overridden with display: none
29                                 // in a stylesheet to whatever the default browser style is
30                                 // for such an element
31                                 if ( this[i].style.display === "" && jQuery.css( this[i], "display" ) === "none" ) {
32                                         jQuery.data(this[i], "olddisplay", defaultDisplay(this[i].nodeName));
33                                 }
34                         }
35
36                         // Set the display of most of the elements in a second loop
37                         // to avoid the constant reflow
38                         for ( i = 0; i < j; i++ ) {
39                                 this[i].style.display = jQuery.data(this[i], "olddisplay") || "";
40                         }
41
42                         return this;
43                 }
44         },
45
46         hide: function( speed, easing, callback ) {
47                 if ( speed || speed === 0 ) {
48                         return this.animate( genFx("hide", 3), speed, easing, callback);
49
50                 } else {
51                         for ( var i = 0, j = this.length; i < j; i++ ) {
52                                 var old = jQuery.data(this[i], "olddisplay");
53                                 if ( !old ) {
54                                         jQuery.data( this[i], "olddisplay", jQuery.css( this[i], "display" ) );
55                                 }
56                         }
57
58                         // Set the display of the elements in a second loop
59                         // to avoid the constant reflow
60                         for ( i = 0; i < j; i++ ) {
61                                 this[i].style.display = "none";
62                         }
63
64                         return this;
65                 }
66         },
67
68         // Save the old toggle function
69         _toggle: jQuery.fn.toggle,
70
71         toggle: function( fn, fn2, callback ) {
72                 var bool = typeof fn === "boolean";
73
74                 if ( jQuery.isFunction(fn) && jQuery.isFunction(fn2) ) {
75                         this._toggle.apply( this, arguments );
76
77                 } else if ( fn == null || bool ) {
78                         this.each(function() {
79                                 var state = bool ? fn : jQuery(this).is(":hidden");
80                                 jQuery(this)[ state ? "show" : "hide" ]();
81                         });
82
83                 } else {
84                         this.animate(genFx("toggle", 3), fn, fn2, callback);
85                 }
86
87                 return this;
88         },
89
90         fadeTo: function( speed, to, easing, callback ) {
91                 return this.filter(":hidden").css("opacity", 0).show().end()
92                                         .animate({opacity: to}, speed, easing, callback);
93         },
94
95         animate: function( prop, speed, easing, callback ) {
96                 var optall = jQuery.speed(speed, easing, callback);
97
98                 if ( jQuery.isEmptyObject( prop ) ) {
99                         return this.each( optall.complete );
100                 }
101
102                 return this[ optall.queue === false ? "each" : "queue" ](function() {
103                         // XXX ‘this’ does not always have a nodeName when running the
104                         // test suite
105
106                         var opt = jQuery.extend({}, optall), p,
107                                 isElement = this.nodeType === 1,
108                                 hidden = isElement && jQuery(this).is(":hidden"),
109                                 self = this;
110
111                         for ( p in prop ) {
112                                 var name = jQuery.camelCase( p );
113
114                                 if ( p !== name ) {
115                                         prop[ name ] = prop[ p ];
116                                         delete prop[ p ];
117                                         p = name;
118                                 }
119
120                                 if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
121                                         return opt.complete.call(this);
122                                 }
123
124                                 if ( isElement && ( p === "height" || p === "width" ) ) {
125                                         // Make sure that nothing sneaks out
126                                         // Record all 3 overflow attributes because IE does not
127                                         // change the overflow attribute when overflowX and
128                                         // overflowY are set to the same value
129                                         opt.overflow = [ this.style.overflow, this.style.overflowX, this.style.overflowY ];
130
131                                         // Set display property to inline-block for height/width
132                                         // animations on inline elements that are having width/height
133                                         // animated
134                                         if ( jQuery.css( this, "display" ) === "inline" &&
135                                                         jQuery.css( this, "float" ) === "none" ) {
136                                                 if ( !jQuery.support.inlineBlockNeedsLayout ) {
137                                                         this.style.display = "inline-block";
138
139                                                 } else {
140                                                         var display = defaultDisplay(this.nodeName);
141
142                                                         // inline-level elements accept inline-block;
143                                                         // block-level elements need to be inline with layout
144                                                         if ( display === "inline" ) {
145                                                                 this.style.display = "inline-block";
146
147                                                         } else {
148                                                                 this.style.display = "inline";
149                                                                 this.style.zoom = 1;
150                                                         }
151                                                 }
152                                         }
153                                 }
154
155                                 if ( jQuery.isArray( prop[p] ) ) {
156                                         // Create (if needed) and add to specialEasing
157                                         (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
158                                         prop[p] = prop[p][0];
159                                 }
160                         }
161
162                         if ( opt.overflow != null ) {
163                                 this.style.overflow = "hidden";
164                         }
165
166                         opt.curAnim = jQuery.extend({}, prop);
167
168                         jQuery.each( prop, function( name, val ) {
169                                 var e = new jQuery.fx( self, opt, name );
170
171                                 if ( rfxtypes.test(val) ) {
172                                         e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
173
174                                 } else {
175                                         var parts = rfxnum.exec(val),
176                                                 start = e.cur(true) || 0;
177
178                                         if ( parts ) {
179                                                 var end = parseFloat( parts[2] ),
180                                                         unit = parts[3] || "px";
181
182                                                 // We need to compute starting value
183                                                 if ( unit !== "px" ) {
184                                                         self.style[ name ] = (end || 1) + unit;
185                                                         start = ((end || 1) / e.cur(true)) * start;
186                                                         self.style[ name ] = start + unit;
187                                                 }
188
189                                                 // If a +=/-= token was provided, we're doing a relative animation
190                                                 if ( parts[1] ) {
191                                                         end = ((parts[1] === "-=" ? -1 : 1) * end) + start;
192                                                 }
193
194                                                 e.custom( start, end, unit );
195
196                                         } else {
197                                                 e.custom( start, val, "" );
198                                         }
199                                 }
200                         });
201
202                         // For JS strict compliance
203                         return true;
204                 });
205         },
206
207         stop: function( clearQueue, gotoEnd ) {
208                 var timers = jQuery.timers;
209
210                 if ( clearQueue ) {
211                         this.queue([]);
212                 }
213
214                 this.each(function() {
215                         // go in reverse order so anything added to the queue during the loop is ignored
216                         for ( var i = timers.length - 1; i >= 0; i-- ) {
217                                 if ( timers[i].elem === this ) {
218                                         if (gotoEnd) {
219                                                 // force the next step to be the last
220                                                 timers[i](true);
221                                         }
222
223                                         timers.splice(i, 1);
224                                 }
225                         }
226                 });
227
228                 // start the next in the queue if the last step wasn't forced
229                 if ( !gotoEnd ) {
230                         this.dequeue();
231                 }
232
233                 return this;
234         }
235
236 });
237
238 function genFx( type, num ) {
239         var obj = {};
240
241         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
242                 obj[ this ] = type;
243         });
244
245         return obj;
246 }
247
248 // Generate shortcuts for custom animations
249 jQuery.each({
250         slideDown: genFx("show", 1),
251         slideUp: genFx("hide", 1),
252         slideToggle: genFx("toggle", 1),
253         fadeIn: { opacity: "show" },
254         fadeOut: { opacity: "hide" }
255 }, function( name, props ) {
256         jQuery.fn[ name ] = function( speed, easing, callback ) {
257                 return this.animate( props, speed, easing, callback );
258         };
259 });
260
261 jQuery.extend({
262         speed: function( speed, easing, fn ) {
263                 var opt = speed && typeof speed === "object" ? jQuery.extend({}, speed) : {
264                         complete: fn || !fn && easing ||
265                                 jQuery.isFunction( speed ) && speed,
266                         duration: speed,
267                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
268                 };
269
270                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
271                         opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[opt.duration] : jQuery.fx.speeds._default;
272
273                 // Queueing
274                 opt.old = opt.complete;
275                 opt.complete = function() {
276                         if ( opt.queue !== false ) {
277                                 jQuery(this).dequeue();
278                         }
279                         if ( jQuery.isFunction( opt.old ) ) {
280                                 opt.old.call( this );
281                         }
282                 };
283
284                 return opt;
285         },
286
287         easing: {
288                 linear: function( p, n, firstNum, diff ) {
289                         return firstNum + diff * p;
290                 },
291                 swing: function( p, n, firstNum, diff ) {
292                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
293                 }
294         },
295
296         timers: [],
297
298         fx: function( elem, options, prop ) {
299                 this.options = options;
300                 this.elem = elem;
301                 this.prop = prop;
302
303                 if ( !options.orig ) {
304                         options.orig = {};
305                 }
306         }
307
308 });
309
310 jQuery.fx.prototype = {
311         // Simple function for setting a style value
312         update: function() {
313                 if ( this.options.step ) {
314                         this.options.step.call( this.elem, this.now, this );
315                 }
316
317                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
318         },
319
320         // Get the current size
321         cur: function() {
322                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
323                         return this.elem[ this.prop ];
324                 }
325
326                 var r = parseFloat( jQuery.css( this.elem, this.prop ) );
327                 return r && r > -10000 ? r : 0;
328         },
329
330         // Start an animation from one number to another
331         custom: function( from, to, unit ) {
332                 this.startTime = jQuery.now();
333                 this.start = from;
334                 this.end = to;
335                 this.unit = unit || this.unit || "px";
336                 this.now = this.start;
337                 this.pos = this.state = 0;
338
339                 var self = this, fx = jQuery.fx;
340                 function t( gotoEnd ) {
341                         return self.step(gotoEnd);
342                 }
343
344                 t.elem = this.elem;
345
346                 if ( t() && jQuery.timers.push(t) && !timerId ) {
347                         timerId = setInterval(fx.tick, fx.interval);
348                 }
349         },
350
351         // Simple 'show' function
352         show: function() {
353                 // Remember where we started, so that we can go back to it later
354                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
355                 this.options.show = true;
356
357                 // Begin the animation
358                 // Make sure that we start at a small width/height to avoid any
359                 // flash of content
360                 this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
361
362                 // Start by showing the element
363                 jQuery( this.elem ).show();
364         },
365
366         // Simple 'hide' function
367         hide: function() {
368                 // Remember where we started, so that we can go back to it later
369                 this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
370                 this.options.hide = true;
371
372                 // Begin the animation
373                 this.custom(this.cur(), 0);
374         },
375
376         // Each step of an animation
377         step: function( gotoEnd ) {
378                 var t = jQuery.now(), done = true;
379
380                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
381                         this.now = this.end;
382                         this.pos = this.state = 1;
383                         this.update();
384
385                         this.options.curAnim[ this.prop ] = true;
386
387                         for ( var i in this.options.curAnim ) {
388                                 if ( this.options.curAnim[i] !== true ) {
389                                         done = false;
390                                 }
391                         }
392
393                         if ( done ) {
394                                 // Reset the overflow
395                                 if ( this.options.overflow != null && !jQuery.support.shrinkWrapBlocks ) {
396                                         var elem = this.elem, options = this.options;
397                                         jQuery.each( [ "", "X", "Y" ], function (index, value) {
398                                                 elem.style[ "overflow" + value ] = options.overflow[index];
399                                         } );
400                                 }
401
402                                 // Hide the element if the "hide" operation was done
403                                 if ( this.options.hide ) {
404                                         jQuery(this.elem).hide();
405                                 }
406
407                                 // Reset the properties, if the item has been hidden or shown
408                                 if ( this.options.hide || this.options.show ) {
409                                         for ( var p in this.options.curAnim ) {
410                                                 jQuery.style( this.elem, p, this.options.orig[p] );
411                                         }
412                                 }
413
414                                 // Execute the complete function
415                                 this.options.complete.call( this.elem );
416                         }
417
418                         return false;
419
420                 } else {
421                         var n = t - this.startTime;
422                         this.state = n / this.options.duration;
423
424                         // Perform the easing function, defaults to swing
425                         var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
426                         var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
427                         this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
428                         this.now = this.start + ((this.end - this.start) * this.pos);
429
430                         // Perform the next step of the animation
431                         this.update();
432                 }
433
434                 return true;
435         }
436 };
437
438 jQuery.extend( jQuery.fx, {
439         tick: function() {
440                 var timers = jQuery.timers;
441
442                 for ( var i = 0; i < timers.length; i++ ) {
443                         if ( !timers[i]() ) {
444                                 timers.splice(i--, 1);
445                         }
446                 }
447
448                 if ( !timers.length ) {
449                         jQuery.fx.stop();
450                 }
451         },
452
453         interval: 13,
454
455         stop: function() {
456                 clearInterval( timerId );
457                 timerId = null;
458         },
459
460         speeds: {
461                 slow: 600,
462                 fast: 200,
463                 // Default speed
464                 _default: 400
465         },
466
467         step: {
468                 opacity: function( fx ) {
469                         jQuery.style( fx.elem, "opacity", fx.now );
470                 },
471
472                 _default: function( fx ) {
473                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
474                                 fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
475                         } else {
476                                 fx.elem[ fx.prop ] = fx.now;
477                         }
478                 }
479         }
480 });
481
482 if ( jQuery.expr && jQuery.expr.filters ) {
483         jQuery.expr.filters.animated = function( elem ) {
484                 return jQuery.grep(jQuery.timers, function( fn ) {
485                         return elem === fn.elem;
486                 }).length;
487         };
488 }
489
490 function defaultDisplay( nodeName ) {
491         if ( !elemdisplay[ nodeName ] ) {
492                 var elem = jQuery("<" + nodeName + ">").appendTo("body"),
493                         display = elem.css("display");
494
495                 elem.remove();
496
497                 if ( display === "none" || display === "" ) {
498                         display = "block";
499                 }
500
501                 elemdisplay[ nodeName ] = display;
502         }
503
504         return elemdisplay[ nodeName ];
505 }
506
507 })( jQuery );