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