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