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