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