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";
51 * Hides each of the set of matched elements if they are shown.
53 * @example $("p").hide()
54 * @before <p>Hello</p>
55 * @result [ <p style="display: none">Hello</p> ]
63 * Hide all matched elements using a graceful animation and firing an
64 * optional callback after completion.
66 * The height, width, and opacity of each of the matched elements
67 * are changed dynamically according to the specified speed.
69 * @example $("p").hide("slow");
71 * @example $("p").hide("slow",function(){
72 * alert("Animation Done.");
77 * @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).
78 * @param Function callback (optional) A function to be executed whenever the animation completes.
80 * @see show(String|Number,Function)
82 hide: function(speed,callback){
83 var visible = this.filter(":visible");
86 height: "hide", width: "hide", opacity: "hide"
89 visible.each(function(){
90 this.oldblock = this.oldblock || jQuery.css(this,"display");
91 if ( this.oldblock == "none" )
92 this.oldblock = "block";
93 this.style.display = "none";
98 // Save the old toggle function
99 _toggle: jQuery.fn.toggle,
102 * Toggles each of the set of matched elements. If they are shown,
103 * toggle makes them hidden. If they are hidden, toggle
106 * @example $("p").toggle()
107 * @before <p>Hello</p><p style="display: none">Hello Again</p>
108 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
114 toggle: function( fn, fn2 ){
115 var args = arguments;
116 return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
117 this._toggle( fn, fn2 ) :
118 this.each(function(){
119 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]
120 .apply( jQuery(this), args );
125 * Reveal all matched elements by adjusting their height and firing an
126 * optional callback after completion.
128 * Only the height is adjusted for this animation, causing all matched
129 * elements to be revealed in a "sliding" manner.
131 * @example $("p").slideDown("slow");
133 * @example $("p").slideDown("slow",function(){
134 * alert("Animation Done.");
139 * @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).
140 * @param Function callback (optional) A function to be executed whenever the animation completes.
142 * @see slideUp(String|Number,Function)
143 * @see slideToggle(String|Number,Function)
145 slideDown: function(speed,callback){
146 return this.animate({height: "show"}, speed, callback);
150 * Hide all matched elements by adjusting their height and firing an
151 * optional callback after completion.
153 * Only the height is adjusted for this animation, causing all matched
154 * elements to be hidden in a "sliding" manner.
156 * @example $("p").slideUp("slow");
158 * @example $("p").slideUp("slow",function(){
159 * alert("Animation Done.");
164 * @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).
165 * @param Function callback (optional) A function to be executed whenever the animation completes.
167 * @see slideDown(String|Number,Function)
168 * @see slideToggle(String|Number,Function)
170 slideUp: function(speed,callback){
171 return this.animate({height: "hide"}, speed, callback);
175 * Toggle the visibility of all matched elements by adjusting their height and firing an
176 * optional callback after completion.
178 * Only the height is adjusted for this animation, causing all matched
179 * elements to be hidden in a "sliding" manner.
181 * @example $("p").slideToggle("slow");
183 * @example $("p").slideToggle("slow",function(){
184 * alert("Animation Done.");
189 * @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).
190 * @param Function callback (optional) A function to be executed whenever the animation completes.
192 * @see slideDown(String|Number,Function)
193 * @see slideUp(String|Number,Function)
195 slideToggle: function(speed, callback){
196 return this.each(function(){
197 var state = jQuery(this).is(":hidden") ? "show" : "hide";
198 jQuery(this).animate({height: state}, speed, callback);
203 * Fade in all matched elements by adjusting their opacity and firing an
204 * optional callback after completion.
206 * Only the opacity is adjusted for this animation, meaning that
207 * all of the matched elements should already have some form of height
208 * and width associated with them.
210 * @example $("p").fadeIn("slow");
212 * @example $("p").fadeIn("slow",function(){
213 * alert("Animation Done.");
218 * @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).
219 * @param Function callback (optional) A function to be executed whenever the animation completes.
221 * @see fadeOut(String|Number,Function)
222 * @see fadeTo(String|Number,Number,Function)
224 fadeIn: function(speed, callback){
225 return this.animate({opacity: "show"}, speed, callback);
229 * Fade out all matched elements by adjusting their opacity and firing an
230 * optional callback after completion.
232 * Only the opacity is adjusted for this animation, meaning that
233 * all of the matched elements should already have some form of height
234 * and width associated with them.
236 * @example $("p").fadeOut("slow");
238 * @example $("p").fadeOut("slow",function(){
239 * alert("Animation Done.");
244 * @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).
245 * @param Function callback (optional) A function to be executed whenever the animation completes.
247 * @see fadeIn(String|Number,Function)
248 * @see fadeTo(String|Number,Number,Function)
250 fadeOut: function(speed, callback){
251 return this.animate({opacity: "hide"}, speed, callback);
255 * Fade the opacity of all matched elements to a specified opacity and firing an
256 * optional callback after completion.
258 * Only the opacity is adjusted for this animation, meaning that
259 * all of the matched elements should already have some form of height
260 * and width associated with them.
262 * @example $("p").fadeTo("slow", 0.5);
264 * @example $("p").fadeTo("slow", 0.5, function(){
265 * alert("Animation Done.");
270 * @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).
271 * @param Number opacity The opacity to fade to (a number from 0 to 1).
272 * @param Function callback (optional) A function to be executed whenever the animation completes.
274 * @see fadeIn(String|Number,Function)
275 * @see fadeOut(String|Number,Function)
277 fadeTo: function(speed,to,callback){
278 return this.animate({opacity: to}, speed, callback);
282 * A function for making your own, custom animations. The key aspect of
283 * this function is the object of style properties that will be animated,
284 * and to what end. Each key within the object represents a style property
285 * that will also be animated (for example: "height", "top", or "opacity").
287 * Note that properties should be specified using camel case
288 * eg. marginLeft instead of margin-left.
290 * The value associated with the key represents to what end the property
291 * will be animated. If a number is provided as the value, then the style
292 * property will be transitioned from its current state to that new number.
293 * Otherwise if the string "hide", "show", or "toggle" is provided, a default
294 * animation will be constructed for that property.
296 * @example $("p").animate({
297 * height: 'toggle', opacity: 'toggle'
300 * @example $("p").animate({
301 * left: 50, opacity: 'show'
304 * @example $("p").animate({
306 * }, "slow", "easein");
307 * @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).
311 * @param Hash params A set of style attributes that you wish to animate, and to what end.
312 * @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).
313 * @param String easing (optional) The name of the easing effect that you want to use (Plugin Required).
314 * @param Function callback (optional) A function to be executed whenever the animation completes.
317 animate: function( prop, speed, easing, callback ) {
318 return this.queue(function(){
320 this.curAnim = jQuery.extend({}, prop);
321 var opt = jQuery.speed(speed, easing, callback);
323 for ( var p in prop ) {
324 var e = new jQuery.fx( this, opt, p );
325 if ( prop[p].constructor == Number )
326 e.custom( e.cur(), prop[p] );
328 e[ prop[p] ]( prop );
338 queue: function(type,fn){
344 return this.each(function(){
348 if ( !this.queue[type] )
349 this.queue[type] = [];
351 this.queue[type].push( fn );
353 if ( this.queue[type].length == 1 )
362 speed: function(speed, easing, fn) {
363 var opt = speed && speed.constructor == Object ? speed : {
364 complete: fn || !fn && easing ||
365 jQuery.isFunction( speed ) && speed,
367 easing: fn && easing || easing && easing.constructor != Function && easing
370 opt.duration = (opt.duration && opt.duration.constructor == Number ?
372 { slow: 600, fast: 200 }[opt.duration]) || 400;
375 opt.old = opt.complete;
376 opt.complete = function(){
377 jQuery.dequeue(this, "fx");
378 if ( jQuery.isFunction( opt.old ) )
379 opt.old.apply( this );
389 dequeue: function(elem,type){
392 if ( elem.queue && elem.queue[type] ) {
394 elem.queue[type].shift();
397 var f = elem.queue[type][0];
399 if ( f ) f.apply( elem );
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.
409 fx: function( elem, options, prop ){
416 // Store display property
417 var oldDisplay = jQuery.css(elem, "display");
419 // Make sure that nothing sneaks out
420 y.overflow = "hidden";
422 // Simple function for setting a style value
425 options.step.apply( elem, [ z.now ] );
427 if ( prop == "opacity" )
428 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
429 else if ( parseInt(z.now) ) // My hate for IE will never die
430 y[prop] = parseInt(z.now) + "px";
432 y.display = "block"; // Set display property to block for animation
435 // Figure out the maximum number to run to
437 return parseFloat( jQuery.css(elem,prop) );
440 // Get the current size
442 var r = parseFloat( jQuery.curCSS(elem, prop) );
443 return r && r > -10000 ? r : z.max();
446 // Start an animation from one number to another
447 z.custom = function(from,to){
448 z.startTime = (new Date()).getTime();
452 z.timer = setInterval(function(){
457 // Simple 'show' function
459 if ( !elem.orig ) elem.orig = {};
461 // Remember where we started, so that we can go back to it later
462 elem.orig[prop] = this.cur();
466 // Begin the animation
467 z.custom(0, elem.orig[prop]);
469 // Stupid IE, look what you made me do
470 if ( prop != "opacity" )
474 // Simple 'hide' function
476 if ( !elem.orig ) elem.orig = {};
478 // Remember where we started, so that we can go back to it later
479 elem.orig[prop] = this.cur();
483 // Begin the animation
484 z.custom(elem.orig[prop], 0);
487 //Simple 'toggle' function
488 z.toggle = function() {
489 if ( !elem.orig ) elem.orig = {};
491 // Remember where we started, so that we can go back to it later
492 elem.orig[prop] = this.cur();
494 if(oldDisplay == "none") {
497 // Stupid IE, look what you made me do
498 if ( prop != "opacity" )
501 // Begin the animation
502 z.custom(0, elem.orig[prop]);
506 // Begin the animation
507 z.custom(elem.orig[prop], 0);
511 // Each step of an animation
512 z.step = function(firstNum, lastNum){
513 var t = (new Date()).getTime();
515 if (t > options.duration + z.startTime) {
517 clearInterval(z.timer);
523 if (elem.curAnim) elem.curAnim[ prop ] = true;
526 for ( var i in elem.curAnim )
527 if ( elem.curAnim[i] !== true )
531 // Reset the overflow
535 y.display = oldDisplay;
536 if (jQuery.css(elem, "display") == "none")
539 // Hide the element if the "hide" operation was done
543 // Reset the properties, if the item has been hidden or shown
544 if ( options.hide || options.show )
545 for ( var p in elem.curAnim )
547 jQuery.attr(y, p, elem.orig[p]);
552 // If a callback was provided, execute it
553 if ( done && jQuery.isFunction( options.complete ) )
554 // Execute the complete function
555 options.complete.apply( elem );
557 var n = t - this.startTime;
558 // Figure out where in the animation we are and set the number
559 var p = n / options.duration;
561 // If the easing function exists, then use it
562 z.now = options.easing && jQuery.easing[options.easing] ?
563 jQuery.easing[options.easing](p, n, firstNum, (lastNum-firstNum), options.duration) :
564 // else use default linear easing
565 ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
567 // Perform the next step of the animation