Fix for #276
[jquery.git] / src / fx / fx.js
1 jQuery.fn.extend({
2
3         // overwrite the old show method
4         _show: jQuery.fn.show,
5         
6         /**
7          * Show all matched elements using a graceful animation.
8          * The height, width, and opacity of each of the matched elements 
9          * are changed dynamically according to the specified speed.
10          *
11          * @example $("p").show("slow");
12          *
13          * @name show
14          * @type jQuery
15          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
16          * @cat Effects/Animations
17          */
18          
19         /**
20          * Show all matched elements using a graceful animation and firing a callback
21          * function after completion.
22          * The height, width, and opacity of each of the matched elements 
23          * are changed dynamically according to the specified speed.
24          *
25          * @example $("p").show("slow",function(){
26          *   alert("Animation Done.");
27          * });
28          *
29          * @name show
30          * @type jQuery
31          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
32          * @param Function callback A function to be executed whenever the animation completes.
33          * @cat Effects/Animations
34          */
35         show: function(speed,callback){
36                 return speed ? this.animate({
37                         height: "show", width: "show", opacity: "show"
38                 }, speed, callback) : this._show();
39         },
40         
41         // Overwrite the old hide method
42         _hide: jQuery.fn.hide,
43         
44         /**
45          * Hide all matched elements using a graceful animation.
46          * The height, width, and opacity of each of the matched elements 
47          * are changed dynamically according to the specified speed.
48          *
49          * @example $("p").hide("slow");
50          *
51          * @name hide
52          * @type jQuery
53          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
54          * @cat Effects/Animations
55          */
56          
57         /**
58          * Hide all matched elements using a graceful animation and firing a callback
59          * function after completion.
60          * The height, width, and opacity of each of the matched elements 
61          * are changed dynamically according to the specified speed.
62          *
63          * @example $("p").hide("slow",function(){
64          *   alert("Animation Done.");
65          * });
66          *
67          * @name hide
68          * @type jQuery
69          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
70          * @param Function callback A function to be executed whenever the animation completes.
71          * @cat Effects/Animations
72          */
73         hide: function(speed,callback){
74                 return speed ? this.animate({
75                         height: "hide", width: "hide", opacity: "hide"
76                 }, speed, callback) : this._hide();
77         },
78         
79         /**
80          * Reveal all matched elements by adjusting their height.
81          * Only the height is adjusted for this animation, causing all matched
82          * elements to be revealed in a "sliding" manner.
83          *
84          * @example $("p").slideDown("slow");
85          *
86          * @name slideDown
87          * @type jQuery
88          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
89          * @cat Effects/Animations
90          */
91          
92         /**
93          * Reveal all matched elements by adjusting their height and firing a callback
94          * function after completion.
95          * Only the height is adjusted for this animation, causing all matched
96          * elements to be revealed in a "sliding" manner.
97          *
98          * @example $("p").slideDown("slow",function(){
99          *   alert("Animation Done.");
100          * });
101          *
102          * @name slideDown
103          * @type jQuery
104          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
105          * @param Function callback A function to be executed whenever the animation completes.
106          * @cat Effects/Animations
107          */
108         slideDown: function(speed,callback){
109                 return this.animate({height: "show"}, speed, callback);
110         },
111         
112         /**
113          * Hide all matched elements by adjusting their height.
114          * Only the height is adjusted for this animation, causing all matched
115          * elements to be hidden in a "sliding" manner.
116          *
117          * @example $("p").slideUp("slow");
118          *
119          * @name slideUp
120          * @type jQuery
121          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
122          * @cat Effects/Animations
123          */
124          
125         /**
126          * Hide all matched elements by adjusting their height and firing a callback
127          * function after completion.
128          * Only the height is adjusted for this animation, causing all matched
129          * elements to be hidden in a "sliding" manner.
130          *
131          * @example $("p").slideUp("slow",function(){
132          *   alert("Animation Done.");
133          * });
134          *
135          * @name slideUp
136          * @type jQuery
137          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
138          * @param Function callback A function to be executed whenever the animation completes.
139          * @cat Effects/Animations
140          */
141         slideUp: function(speed,callback){
142                 return this.animate({height: "hide"}, speed, callback);
143         },
144
145         /**
146          * Toggle the visibility of all matched elements by adjusting their height.
147          * Only the height is adjusted for this animation, causing all matched
148          * elements to be hidden in a "sliding" manner.
149          *
150          * @example $("p").slideToggle("slow");
151          *
152          * @name slideToggle
153          * @type jQuery
154          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
155          * @cat Effects/Animations
156          */
157          
158         /**
159          * Toggle the visibility of all matched elements by adjusting their height
160          * and firing a callback function after completion.
161          * Only the height is adjusted for this animation, causing all matched
162          * elements to be hidden in a "sliding" manner.
163          *
164          * @example $("p").slideToggle("slow",function(){
165          *   alert("Animation Done.");
166          * });
167          *
168          * @name slideToggle
169          * @type jQuery
170          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
171          * @param Function callback A function to be executed whenever the animation completes.
172          * @cat Effects/Animations
173          */
174         slideToggle: function(speed,callback){
175                 return this.each(function(){
176                         var state = jQuery(this).is(":hidden") ? "show" : "hide";
177                         jQuery(this).animate({height: state}, speed, callback);
178                 });
179         },
180         
181         /**
182          * Fade in all matched elements by adjusting their opacity.
183          * Only the opacity is adjusted for this animation, meaning that
184          * all of the matched elements should already have some form of height
185          * and width associated with them.
186          *
187          * @example $("p").fadeIn("slow");
188          *
189          * @name fadeIn
190          * @type jQuery
191          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
192          * @cat Effects/Animations
193          */
194          
195         /**
196          * Fade in all matched elements by adjusting their opacity and firing a 
197          * callback function after completion.
198          * Only the opacity is adjusted for this animation, meaning that
199          * all of the matched elements should already have some form of height
200          * and width associated with them.
201          *
202          * @example $("p").fadeIn("slow",function(){
203          *   alert("Animation Done.");
204          * });
205          *
206          * @name fadeIn
207          * @type jQuery
208          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
209          * @param Function callback A function to be executed whenever the animation completes.
210          * @cat Effects/Animations
211          */
212         fadeIn: function(speed,callback){
213                 return this.animate({opacity: "show"}, speed, callback);
214         },
215         
216         /**
217          * Fade out all matched elements by adjusting their opacity.
218          * Only the opacity is adjusted for this animation, meaning that
219          * all of the matched elements should already have some form of height
220          * and width associated with them.
221          *
222          * @example $("p").fadeOut("slow");
223          *
224          * @name fadeOut
225          * @type jQuery
226          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
227          * @cat Effects/Animations
228          */
229          
230         /**
231          * Fade out all matched elements by adjusting their opacity and firing a 
232          * callback function after completion.
233          * Only the opacity is adjusted for this animation, meaning that
234          * all of the matched elements should already have some form of height
235          * and width associated with them.
236          *
237          * @example $("p").fadeOut("slow",function(){
238          *   alert("Animation Done.");
239          * });
240          *
241          * @name fadeOut
242          * @type jQuery
243          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
244          * @param Function callback A function to be executed whenever the animation completes.
245          * @cat Effects/Animations
246          */
247         fadeOut: function(speed,callback){
248                 return this.animate({opacity: "hide"}, speed, callback);
249         },
250         
251         /**
252          * Fade the opacity of all matched elements to a specified opacity.
253          * Only the opacity is adjusted for this animation, meaning that
254          * all of the matched elements should already have some form of height
255          * and width associated with them.
256          *
257          * @example $("p").fadeTo("slow", 0.5);
258          *
259          * @name fadeTo
260          * @type jQuery
261          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
262          * @param Number opacity The opacity to fade to (a number from 0 to 1).
263          * @cat Effects/Animations
264          */
265          
266         /**
267          * Fade the opacity of all matched elements to a specified opacity and 
268          * firing a callback function after completion.
269          * Only the opacity is adjusted for this animation, meaning that
270          * all of the matched elements should already have some form of height
271          * and width associated with them.
272          *
273          * @example $("p").fadeTo("slow", 0.5, function(){
274          *   alert("Animation Done.");
275          * });
276          *
277          * @name fadeTo
278          * @type jQuery
279          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
280          * @param Number opacity The opacity to fade to (a number from 0 to 1).
281          * @param Function callback A function to be executed whenever the animation completes.
282          * @cat Effects/Animations
283          */
284         fadeTo: function(speed,to,callback){
285                 return this.animate({opacity: to}, speed, callback);
286         },
287         
288         /**
289          * A function for making your own, custom, animations. The key aspect of
290          * this function is the object of style properties that will be animated,
291          * and to what end. Each key within the object represents a style property
292          * that will also be animated (for example: "height", "top", or "opacity").
293          *
294          * The value associated with the key represents to what end the property
295          * will be animated. If a number is provided as the value, then the style
296          * property will be transitioned from its current state to that new number.
297          * Oterwise if the string "hide", "show", or "toggle" is provided, a default
298          * animation will be constructed for that property.
299          *
300          * @example $("p").animate({
301          *   height: 'toggle', opacity: 'toggle'
302          * }, "slow");
303          *
304          * @example $("p").animate({
305          *   left: 50, opacity: 'show'
306          * }, 500);
307          *
308          * @test stop();
309          * var hash = {opacity: 'show'};
310          * var hashCopy = $.extend({}, hash);
311          * $('#foo').animate(hash, 'fast', function() {
312          *  ok( hash.opacity == hashCopy.opacity, 'Check if animate changed the hash parameter' );
313          *  start();
314          * });
315          *
316          * @name animate
317          * @type jQuery
318          * @param Hash params A set of style attributes that you wish to animate, and to what end.
319          * @param Object speed A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
320          * @param Function callback A function to be executed whenever the animation completes.
321          * @cat Effects/Animations
322          */
323         animate: function(prop,speed,callback) {
324                 return this.queue(function(){
325                 
326                         this.curAnim = jQuery.extend({}, prop);
327                         
328                         for ( var p in prop ) {
329                                 var e = new jQuery.fx( this, jQuery.speed(speed,callback), p );
330                                 if ( prop[p].constructor == Number )
331                                         e.custom( e.cur(), prop[p] );
332                                 else
333                                         e[ prop[p] ]( prop );
334                         }
335                         
336                 });
337         },
338         
339         /**
340          *
341          * @private
342          */
343         queue: function(type,fn){
344                 if ( !fn ) {
345                         fn = type;
346                         type = "fx";
347                 }
348         
349                 return this.each(function(){
350                         if ( !this.queue )
351                                 this.queue = {};
352         
353                         if ( !this.queue[type] )
354                                 this.queue[type] = [];
355         
356                         this.queue[type].push( fn );
357                 
358                         if ( this.queue[type].length == 1 )
359                                 fn.apply(this);
360                 });
361         }
362
363 });
364
365 jQuery.extend({
366         
367         speed: function(s,o) {
368                 o = o || {};
369                 
370                 if ( o.constructor == Function )
371                         o = { complete: o };
372                 
373                 var ss = { slow: 600, fast: 200 };
374                 o.duration = (s && s.constructor == Number ? s : ss[s]) || 400;
375         
376                 // Queueing
377                 o.oldComplete = o.complete;
378                 o.complete = function(){
379                         jQuery.dequeue(this, "fx");
380                         if ( o.oldComplete && o.oldComplete.constructor == Function )
381                                 o.oldComplete.apply( this );
382                 };
383         
384                 return o;
385         },
386         
387         queue: {},
388         
389         dequeue: function(elem,type){
390                 type = type || "fx";
391         
392                 if ( elem.queue && elem.queue[type] ) {
393                         // Remove self
394                         elem.queue[type].shift();
395         
396                         // Get next function
397                         var f = elem.queue[type][0];
398                 
399                         if ( f ) f.apply( elem );
400                 }
401         },
402
403         /*
404          * I originally wrote fx() as a clone of moo.fx and in the process
405          * of making it small in size the code became illegible to sane
406          * people. You've been warned.
407          */
408         
409         fx: function( elem, options, prop ){
410
411                 var z = this;
412
413                 // The users options
414                 z.o = {
415                         duration: options.duration || 400,
416                         complete: options.complete,
417                         step: options.step
418                 };
419
420                 // The element
421                 z.el = elem;
422
423                 // The styles
424                 var y = z.el.style;
425
426                 // Simple function for setting a style value
427                 z.a = function(){
428                         if ( options.step )
429                                 options.step.apply( elem, [ z.now ] );
430
431                         if ( prop == "opacity" )
432                                 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
433                         else if ( parseInt(z.now) ) // My hate for IE will never die
434                                 y[prop] = parseInt(z.now) + "px";
435
436                         y.display = "block";
437                 };
438
439                 // Figure out the maximum number to run to
440                 z.max = function(){
441                         return parseFloat( jQuery.css(z.el,prop) );
442                 };
443
444                 // Get the current size
445                 z.cur = function(){
446                         var r = parseFloat( jQuery.curCSS(z.el, prop) );
447                         return r && r > -10000 ? r : z.max();
448                 };
449
450                 // Start an animation from one number to another
451                 z.custom = function(from,to){
452                         z.startTime = (new Date()).getTime();
453                         z.now = from;
454                         z.a();
455
456                         z.timer = setInterval(function(){
457                                 z.step(from, to);
458                         }, 13);
459                 };
460
461                 // Simple 'show' function
462                 z.show = function(){
463                         if ( !z.el.orig ) z.el.orig = {};
464
465                         // Remember where we started, so that we can go back to it later
466                         z.el.orig[prop] = this.cur();
467
468                         z.o.show = true;
469
470                         // Begin the animation
471                         z.custom(0, z.el.orig[prop]);
472
473                         // Stupid IE, look what you made me do
474                         if ( prop != "opacity" )
475                                 y[prop] = "1px";
476                 };
477
478                 // Simple 'hide' function
479                 z.hide = function(){
480                         if ( !z.el.orig ) z.el.orig = {};
481
482                         // Remember where we started, so that we can go back to it later
483                         z.el.orig[prop] = this.cur();
484
485                         z.o.hide = true;
486
487                         // Begin the animation
488                         z.custom(z.el.orig[prop], 0);
489                 };
490
491                 // Make sure that nothing sneaks out
492                 y.overflow = "hidden";
493
494                 // Each step of an animation
495                 z.step = function(firstNum, lastNum){
496                         var t = (new Date()).getTime();
497
498                         if (t > z.o.duration + z.startTime) {
499                                 // Stop the timer
500                                 clearInterval(z.timer);
501                                 z.timer = null;
502
503                                 z.now = lastNum;
504                                 z.a();
505
506                                 z.el.curAnim[ prop ] = true;
507
508                                 var done = true;
509                                 for ( var i in z.el.curAnim )
510                                         if ( z.el.curAnim[i] !== true )
511                                                 done = false;
512
513                                 if ( done ) {
514                                         // Reset the overflow
515                                         y.overflow = '';
516
517                                         // Hide the element if the "hide" operation was done
518                                         if ( z.o.hide ) 
519                                                 y.display = 'none';
520                                         // Otherwise reset the display property
521                                         else if ( z.o.show )
522                                                 y.display = '';
523
524                                         // Reset the properties, if the item has been hidden or shown
525                                         if ( z.o.hide || z.o.show )
526                                                 for ( var p in z.el.curAnim )
527                                                         if (p == "opacity")
528                                                                 jQuery.attr(y, p, z.el.orig[p]);
529                                                         else
530                                                                 y[p] = '';
531                                 }
532
533                                 // If a callback was provided, execute it
534                                 if( done && z.o.complete && z.o.complete.constructor == Function )
535                                         // Execute the complete function
536                                         z.o.complete.apply( z.el );
537                         } else {
538                                 // Figure out where in the animation we are and set the number
539                                 var p = (t - this.startTime) / z.o.duration;
540                                 z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
541
542                                 // Perform the next step of the animation
543                                 z.a();
544                         }
545                 };
546         
547         }
548
549 });