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