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