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