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