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