Added a performance improvement to .hide()/.show() that helps to prevent constant...
[jquery.git] / src / fx.js
1 var elemdisplay = {},
2         timerId,
3         fxAttrs = [
4                 // height animations
5                 [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
6                 // width animations
7                 [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
8                 // opacity animations
9                 [ "opacity" ]
10         ];
11
12 function genFx( type, num ){
13         var obj = {};
14         jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function(){
15                 obj[ this ] = type;
16         });
17         return obj;
18 }
19
20 jQuery.fn.extend({
21         show: function(speed,callback){
22                 if ( speed ) {
23                         return this.animate( genFx("show", 3), speed, callback);
24                 } else {
25                         for ( var i = 0, l = this.length; i < l; i++ ){
26                                 var old = jQuery.data(this[i], "olddisplay");
27                                 
28                                 this[i].style.display = old || "";
29                                 
30                                 if ( jQuery.css(this[i], "display") === "none" ) {
31                                         var tagName = this[i].tagName, display;
32                                         
33                                         if ( elemdisplay[ tagName ] ) {
34                                                 display = elemdisplay[ tagName ];
35                                         } else {
36                                                 var elem = jQuery("<" + tagName + " />").appendTo("body");
37                                                 
38                                                 display = elem.css("display");
39                                                 if ( display === "none" )
40                                                         display = "block";
41                                                 
42                                                 elem.remove();
43                                                 
44                                                 elemdisplay[ tagName ] = display;
45                                         }
46                                         
47                                         jQuery.data(this[i], "olddisplay", display);
48                                 }
49
50                                 // Set the display of the elements in a second loop
51                                 // to avoid the constant reflow
52                                 for ( var i = 0, l = this.length; i < l; i++ ){
53                                         this[i].style.display = jQuery.data(this[i], "olddisplay");
54                                 }
55                         }
56                         
57                         return this;
58                 }
59         },
60
61         hide: function(speed,callback){
62                 if ( speed ) {
63                         return this.animate( genFx("hide", 3), speed, callback);
64                 } else {
65                         for ( var i = 0, l = this.length; i < l; i++ ){
66                                 var old = jQuery.data(this[i], "olddisplay");
67                                 if ( !old && old !== "none" )
68                                         jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display"));
69                         }
70
71                         // Set the display of the elements in a second loop
72                         // to avoid the constant reflow
73                         for ( var i = 0, l = this.length; i < l; i++ ){
74                                 this[i].style.display = "none";
75                         }
76
77                         return this;
78                 }
79         },
80
81         // Save the old toggle function
82         _toggle: jQuery.fn.toggle,
83
84         toggle: function( fn, fn2 ){
85                 var bool = typeof fn === "boolean";
86
87                 return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
88                         this._toggle.apply( this, arguments ) :
89                         fn == null || bool ?
90                                 this.each(function(){
91                                         var state = bool ? fn : jQuery(this).is(":hidden");
92                                         jQuery(this)[ state ? "show" : "hide" ]();
93                                 }) :
94                                 this.animate(genFx("toggle", 3), fn, fn2);
95         },
96
97         fadeTo: function(speed,to,callback){
98                 return this.animate({opacity: to}, speed, callback);
99         },
100
101         animate: function( prop, speed, easing, callback ) {
102                 var optall = jQuery.speed(speed, easing, callback);
103
104                 return this[ optall.queue === false ? "each" : "queue" ](function(){
105                 
106                         var opt = jQuery.extend({}, optall), p,
107                                 hidden = this.nodeType == 1 && jQuery(this).is(":hidden"),
108                                 self = this;
109         
110                         for ( p in prop ) {
111                                 if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
112                                         return opt.complete.call(this);
113
114                                 if ( ( p == "height" || p == "width" ) && this.style ) {
115                                         // Store display property
116                                         opt.display = jQuery.css(this, "display");
117
118                                         // Make sure that nothing sneaks out
119                                         opt.overflow = this.style.overflow;
120                                 }
121                         }
122
123                         if ( opt.overflow != null )
124                                 this.style.overflow = "hidden";
125
126                         opt.curAnim = jQuery.extend({}, prop);
127
128                         jQuery.each( prop, function(name, val){
129                                 var e = new jQuery.fx( self, opt, name );
130
131                                 if ( /toggle|show|hide/.test(val) )
132                                         e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
133                                 else {
134                                         var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
135                                                 start = e.cur(true) || 0;
136
137                                         if ( parts ) {
138                                                 var end = parseFloat(parts[2]),
139                                                         unit = parts[3] || "px";
140
141                                                 // We need to compute starting value
142                                                 if ( unit != "px" ) {
143                                                         self.style[ name ] = (end || 1) + unit;
144                                                         start = ((end || 1) / e.cur(true)) * start;
145                                                         self.style[ name ] = start + unit;
146                                                 }
147
148                                                 // If a +=/-= token was provided, we're doing a relative animation
149                                                 if ( parts[1] )
150                                                         end = ((parts[1] == "-=" ? -1 : 1) * end) + start;
151
152                                                 e.custom( start, end, unit );
153                                         } else
154                                                 e.custom( start, val, "" );
155                                 }
156                         });
157
158                         // For JS strict compliance
159                         return true;
160                 });
161         },
162
163         stop: function(clearQueue, gotoEnd){
164                 var timers = jQuery.timers;
165
166                 if (clearQueue)
167                         this.queue([]);
168
169                 this.each(function(){
170                         // go in reverse order so anything added to the queue during the loop is ignored
171                         for ( var i = timers.length - 1; i >= 0; i-- )
172                                 if ( timers[i].elem == this ) {
173                                         if (gotoEnd)
174                                                 // force the next step to be the last
175                                                 timers[i](true);
176                                         timers.splice(i, 1);
177                                 }
178                 });
179
180                 // start the next in the queue if the last step wasn't forced
181                 if (!gotoEnd)
182                         this.dequeue();
183
184                 return this;
185         }
186
187 });
188
189 // Generate shortcuts for custom animations
190 jQuery.each({
191         slideDown: genFx("show", 1),
192         slideUp: genFx("hide", 1),
193         slideToggle: genFx("toggle", 1),
194         fadeIn: { opacity: "show" },
195         fadeOut: { opacity: "hide" }
196 }, function( name, props ){
197         jQuery.fn[ name ] = function( speed, callback ){
198                 return this.animate( props, speed, callback );
199         };
200 });
201
202 jQuery.extend({
203
204         speed: function(speed, easing, fn) {
205                 var opt = typeof speed === "object" ? speed : {
206                         complete: fn || !fn && easing ||
207                                 jQuery.isFunction( speed ) && speed,
208                         duration: speed,
209                         easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
210                 };
211
212                 opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
213                         jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
214
215                 // Queueing
216                 opt.old = opt.complete;
217                 opt.complete = function(){
218                         if ( opt.queue !== false )
219                                 jQuery(this).dequeue();
220                         if ( jQuery.isFunction( opt.old ) )
221                                 opt.old.call( this );
222                 };
223
224                 return opt;
225         },
226
227         easing: {
228                 linear: function( p, n, firstNum, diff ) {
229                         return firstNum + diff * p;
230                 },
231                 swing: function( p, n, firstNum, diff ) {
232                         return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
233                 }
234         },
235
236         timers: [],
237
238         fx: function( elem, options, prop ){
239                 this.options = options;
240                 this.elem = elem;
241                 this.prop = prop;
242
243                 if ( !options.orig )
244                         options.orig = {};
245         }
246
247 });
248
249 jQuery.fx.prototype = {
250
251         // Simple function for setting a style value
252         update: function(){
253                 if ( this.options.step )
254                         this.options.step.call( this.elem, this.now, this );
255
256                 (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
257
258                 // Set display property to block for height/width animations
259                 if ( ( this.prop == "height" || this.prop == "width" ) && this.elem.style )
260                         this.elem.style.display = "block";
261         },
262
263         // Get the current size
264         cur: function(force){
265                 if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) )
266                         return this.elem[ this.prop ];
267
268                 var r = parseFloat(jQuery.css(this.elem, this.prop, force));
269                 return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
270         },
271
272         // Start an animation from one number to another
273         custom: function(from, to, unit){
274                 this.startTime = now();
275                 this.start = from;
276                 this.end = to;
277                 this.unit = unit || this.unit || "px";
278                 this.now = this.start;
279                 this.pos = this.state = 0;
280
281                 var self = this;
282                 function t(gotoEnd){
283                         return self.step(gotoEnd);
284                 }
285
286                 t.elem = this.elem;
287
288                 if ( t() && jQuery.timers.push(t) == 1 ) {
289                         timerId = setInterval(function(){
290                                 var timers = jQuery.timers;
291
292                                 for ( var i = 0; i < timers.length; i++ )
293                                         if ( !timers[i]() )
294                                                 timers.splice(i--, 1);
295
296                                 if ( !timers.length ) {
297                                         clearInterval( timerId );
298                                 }
299                         }, 13);
300                 }
301         },
302
303         // Simple 'show' function
304         show: function(){
305                 // Remember where we started, so that we can go back to it later
306                 this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
307                 this.options.show = true;
308
309                 // Begin the animation
310                 // Make sure that we start at a small width/height to avoid any
311                 // flash of content
312                 this.custom(this.prop == "width" || this.prop == "height" ? 1 : 0, this.cur());
313
314                 // Start by showing the element
315                 jQuery(this.elem).show();
316         },
317
318         // Simple 'hide' function
319         hide: function(){
320                 // Remember where we started, so that we can go back to it later
321                 this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
322                 this.options.hide = true;
323
324                 // Begin the animation
325                 this.custom(this.cur(), 0);
326         },
327
328         // Each step of an animation
329         step: function(gotoEnd){
330                 var t = now();
331
332                 if ( gotoEnd || t >= this.options.duration + this.startTime ) {
333                         this.now = this.end;
334                         this.pos = this.state = 1;
335                         this.update();
336
337                         this.options.curAnim[ this.prop ] = true;
338
339                         var done = true;
340                         for ( var i in this.options.curAnim )
341                                 if ( this.options.curAnim[i] !== true )
342                                         done = false;
343
344                         if ( done ) {
345                                 if ( this.options.display != null ) {
346                                         // Reset the overflow
347                                         this.elem.style.overflow = this.options.overflow;
348
349                                         // Reset the display
350                                         this.elem.style.display = this.options.display;
351                                         if ( jQuery.css(this.elem, "display") == "none" )
352                                                 this.elem.style.display = "block";
353                                 }
354
355                                 // Hide the element if the "hide" operation was done
356                                 if ( this.options.hide )
357                                         jQuery(this.elem).hide();
358
359                                 // Reset the properties, if the item has been hidden or shown
360                                 if ( this.options.hide || this.options.show )
361                                         for ( var p in this.options.curAnim )
362                                                 jQuery.attr(this.elem.style, p, this.options.orig[p]);
363                                         
364                                 // Execute the complete function
365                                 this.options.complete.call( this.elem );
366                         }
367
368                         return false;
369                 } else {
370                         var n = t - this.startTime;
371                         this.state = n / this.options.duration;
372
373                         // Perform the easing function, defaults to swing
374                         this.pos = jQuery.easing[this.options.easing || (jQuery.easing.swing ? "swing" : "linear")](this.state, n, 0, 1, this.options.duration);
375                         this.now = this.start + ((this.end - this.start) * this.pos);
376
377                         // Perform the next step of the animation
378                         this.update();
379                 }
380
381                 return true;
382         }
383
384 };
385
386 jQuery.extend( jQuery.fx, {
387         speeds:{
388                 slow: 600,
389                 fast: 200,
390                 // Default speed
391                 _default: 400
392         },
393         step: {
394
395                 opacity: function(fx){
396                         jQuery.attr(fx.elem.style, "opacity", fx.now);
397                 },
398
399                 _default: function(fx){
400                         if ( fx.elem.style && fx.elem.style[ fx.prop ] != null )
401                                 fx.elem.style[ fx.prop ] = fx.now + fx.unit;
402                         else
403                                 fx.elem[ fx.prop ] = fx.now;
404                 }
405         }
406 });