4 * Displays each of the set of matched elements if they are hidden.
6 * @example $("p").show()
7 * @before <p style="display: none">Hello</p>
8 * @result [ <p style="display: block">Hello</p> ]
16 * Show all matched elements using a graceful animation and firing an
17 * optional callback after completion.
19 * The height, width, and opacity of each of the matched elements
20 * are changed dynamically according to the specified speed.
22 * @example $("p").show("slow");
24 * @example $("p").show("slow",function(){
25 * alert("Animation Done.");
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.
33 * @see hide(String|Number,Function)
35 show: function(speed,callback){
36 var hidden = this.filter(":hidden");
39 height: "show", width: "show", opacity: "show"
42 hidden.each(function(){
43 this.style.display = this.oldblock ? this.oldblock : "";
44 if ( jQuery.css(this,"display") == "none" )
45 this.style.display = "block";
50 * Hides each of the set of matched elements if they are shown.
52 * @example $("p").hide()
53 * @before <p>Hello</p>
54 * @result [ <p style="display: none">Hello</p> ]
62 * Hide all matched elements using a graceful animation and firing an
63 * optional callback after completion.
65 * The height, width, and opacity of each of the matched elements
66 * are changed dynamically according to the specified speed.
68 * @example $("p").hide("slow");
70 * @example $("p").hide("slow",function(){
71 * alert("Animation Done.");
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.
79 * @see show(String|Number,Function)
81 hide: function(speed,callback){
82 var visible = this.filter(":visible");
85 height: "hide", width: "hide", opacity: "hide"
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";
96 // Save the old toggle function
97 _toggle: jQuery.fn.toggle,
100 * Toggles each of the set of matched elements. If they are shown,
101 * toggle makes them hidden. If they are hidden, toggle
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> ]
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 );
123 * Reveal all matched elements by adjusting their height and firing an
124 * optional callback after completion.
126 * Only the height is adjusted for this animation, causing all matched
127 * elements to be revealed in a "sliding" manner.
129 * @example $("p").slideDown("slow");
131 * @example $("p").slideDown("slow",function(){
132 * alert("Animation Done.");
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.
140 * @see slideUp(String|Number,Function)
141 * @see slideToggle(String|Number,Function)
143 slideDown: function(speed,callback){
144 return this.animate({height: "show"}, speed, callback);
148 * Hide all matched elements by adjusting their height and firing an
149 * optional callback after completion.
151 * Only the height is adjusted for this animation, causing all matched
152 * elements to be hidden in a "sliding" manner.
154 * @example $("p").slideUp("slow");
156 * @example $("p").slideUp("slow",function(){
157 * alert("Animation Done.");
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.
165 * @see slideDown(String|Number,Function)
166 * @see slideToggle(String|Number,Function)
168 slideUp: function(speed,callback){
169 return this.animate({height: "hide"}, speed, callback);
173 * Toggle the visibility of all matched elements by adjusting their height and firing an
174 * optional callback after completion.
176 * Only the height is adjusted for this animation, causing all matched
177 * elements to be hidden in a "sliding" manner.
179 * @example $("p").slideToggle("slow");
181 * @example $("p").slideToggle("slow",function(){
182 * alert("Animation Done.");
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.
190 * @see slideDown(String|Number,Function)
191 * @see slideUp(String|Number,Function)
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);
201 * Fade in all matched elements by adjusting their opacity and firing an
202 * optional callback after completion.
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.
208 * @example $("p").fadeIn("slow");
210 * @example $("p").fadeIn("slow",function(){
211 * alert("Animation Done.");
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.
219 * @see fadeOut(String|Number,Function)
220 * @see fadeTo(String|Number,Number,Function)
222 fadeIn: function(speed, callback){
223 return this.animate({opacity: "show"}, speed, callback);
227 * Fade out all matched elements by adjusting their opacity and firing an
228 * optional callback after completion.
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.
234 * @example $("p").fadeOut("slow");
236 * @example $("p").fadeOut("slow",function(){
237 * alert("Animation Done.");
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.
245 * @see fadeIn(String|Number,Function)
246 * @see fadeTo(String|Number,Number,Function)
248 fadeOut: function(speed, callback){
249 return this.animate({opacity: "hide"}, speed, callback);
253 * Fade the opacity of all matched elements to a specified opacity and firing an
254 * optional callback after completion.
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.
260 * @example $("p").fadeTo("slow", 0.5);
262 * @example $("p").fadeTo("slow", 0.5, function(){
263 * alert("Animation Done.");
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.
272 * @see fadeIn(String|Number,Function)
273 * @see fadeOut(String|Number,Function)
275 fadeTo: function(speed,to,callback){
276 return this.animate({opacity: to}, speed, callback);
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").
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.
291 * @example $("p").animate({
292 * height: 'toggle', opacity: 'toggle'
295 * @example $("p").animate({
296 * left: 50, opacity: 'show'
299 * @example $("p").animate({
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).
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.
312 animate: function( prop, speed, easing, callback ) {
313 return this.queue(function(){
315 this.curAnim = jQuery.extend({}, prop);
316 var opt = jQuery.speed(speed, easing, callback);
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] );
323 e[ prop[p] ]( prop );
333 queue: function(type,fn){
339 return this.each(function(){
343 if ( !this.queue[type] )
344 this.queue[type] = [];
346 this.queue[type].push( fn );
348 if ( this.queue[type].length == 1 )
357 speed: function(speed, easing, fn) {
358 var opt = speed && speed.constructor == Object ? speed : {
359 complete: fn || !fn && easing ||
360 jQuery.isFunction( speed ) && speed,
362 easing: fn && easing || easing && easing.constructor != Function && easing
365 opt.duration = (opt.duration && opt.duration.constructor == Number ?
367 { slow: 600, fast: 200 }[opt.duration]) || 400;
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 );
384 dequeue: function(elem,type){
387 if ( elem.queue && elem.queue[type] ) {
389 elem.queue[type].shift();
392 var f = elem.queue[type][0];
394 if ( f ) f.apply( elem );
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.
404 fx: function( elem, options, prop ){
411 // Store display property
412 var oldDisplay = jQuery.css(elem, "display");
414 // Set display property to block for animation
417 // Make sure that nothing sneaks out
418 y.overflow = "hidden";
420 // Simple function for setting a style value
423 options.step.apply( elem, [ z.now ] );
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";
431 // Figure out the maximum number to run to
433 return parseFloat( jQuery.css(elem,prop) );
436 // Get the current size
438 var r = parseFloat( jQuery.curCSS(elem, prop) );
439 return r && r > -10000 ? r : z.max();
442 // Start an animation from one number to another
443 z.custom = function(from,to){
444 z.startTime = (new Date()).getTime();
448 z.timer = setInterval(function(){
453 // Simple 'show' function
455 if ( !elem.orig ) elem.orig = {};
457 // Remember where we started, so that we can go back to it later
458 elem.orig[prop] = this.cur();
462 // Begin the animation
463 z.custom(0, elem.orig[prop]);
465 // Stupid IE, look what you made me do
466 if ( prop != "opacity" )
470 // Simple 'hide' function
472 if ( !elem.orig ) elem.orig = {};
474 // Remember where we started, so that we can go back to it later
475 elem.orig[prop] = this.cur();
479 // Begin the animation
480 z.custom(elem.orig[prop], 0);
483 //Simple 'toggle' function
484 z.toggle = function() {
485 if ( !elem.orig ) elem.orig = {};
487 // Remember where we started, so that we can go back to it later
488 elem.orig[prop] = this.cur();
490 if(oldDisplay == "none") {
493 // Stupid IE, look what you made me do
494 if ( prop != "opacity" )
497 // Begin the animation
498 z.custom(0, elem.orig[prop]);
502 // Begin the animation
503 z.custom(elem.orig[prop], 0);
507 // Each step of an animation
508 z.step = function(firstNum, lastNum){
509 var t = (new Date()).getTime();
511 if (t > options.duration + z.startTime) {
513 clearInterval(z.timer);
519 if (elem.curAnim) elem.curAnim[ prop ] = true;
522 for ( var i in elem.curAnim )
523 if ( elem.curAnim[i] !== true )
527 // Reset the overflow
531 y.display = oldDisplay;
532 if (jQuery.css(elem, "display") == "none")
535 // Hide the element if the "hide" operation was done
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 )
543 jQuery.attr(y, p, elem.orig[p]);
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 );
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;
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;
563 // Perform the next step of the animation