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