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 ){
116 this._toggle( fn, fn2 ) :
117 this.each(function(){
118 jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]
119 .apply( jQuery(this), arguments );
124 * Reveal all matched elements by adjusting their height and firing an
125 * optional callback after completion.
127 * Only the height is adjusted for this animation, causing all matched
128 * elements to be revealed in a "sliding" manner.
130 * @example $("p").slideDown("slow");
132 * @example $("p").slideDown("slow",function(){
133 * alert("Animation Done.");
138 * @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).
139 * @param Function callback (optional) A function to be executed whenever the animation completes.
141 * @see slideUp(String|Number,Function)
142 * @see slideToggle(String|Number,Function)
144 slideDown: function(speed,callback){
145 return this.animate({height: "show"}, speed, callback);
149 * Hide all matched elements by adjusting their height and firing an
150 * optional callback after completion.
152 * Only the height is adjusted for this animation, causing all matched
153 * elements to be hidden in a "sliding" manner.
155 * @example $("p").slideUp("slow");
157 * @example $("p").slideUp("slow",function(){
158 * alert("Animation Done.");
163 * @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).
164 * @param Function callback (optional) A function to be executed whenever the animation completes.
166 * @see slideDown(String|Number,Function)
167 * @see slideToggle(String|Number,Function)
169 slideUp: function(speed,callback){
170 return this.animate({height: "hide"}, speed, callback);
174 * Toggle the visibility of all matched elements by adjusting their height and firing an
175 * optional callback after completion.
177 * Only the height is adjusted for this animation, causing all matched
178 * elements to be hidden in a "sliding" manner.
180 * @example $("p").slideToggle("slow");
182 * @example $("p").slideToggle("slow",function(){
183 * alert("Animation Done.");
188 * @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).
189 * @param Function callback (optional) A function to be executed whenever the animation completes.
191 * @see slideDown(String|Number,Function)
192 * @see slideUp(String|Number,Function)
194 slideToggle: function(speed, callback){
195 return this.each(function(){
196 var state = jQuery(this).is(":hidden") ? "show" : "hide";
197 jQuery(this).animate({height: state}, speed, callback);
202 * Fade in all matched elements by adjusting their opacity and firing an
203 * optional callback after completion.
205 * Only the opacity is adjusted for this animation, meaning that
206 * all of the matched elements should already have some form of height
207 * and width associated with them.
209 * @example $("p").fadeIn("slow");
211 * @example $("p").fadeIn("slow",function(){
212 * alert("Animation Done.");
217 * @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).
218 * @param Function callback (optional) A function to be executed whenever the animation completes.
220 * @see fadeOut(String|Number,Function)
221 * @see fadeTo(String|Number,Number,Function)
223 fadeIn: function(speed, callback){
224 return this.animate({opacity: "show"}, speed, callback);
228 * Fade out all matched elements by adjusting their opacity and firing an
229 * optional callback after completion.
231 * Only the opacity is adjusted for this animation, meaning that
232 * all of the matched elements should already have some form of height
233 * and width associated with them.
235 * @example $("p").fadeOut("slow");
237 * @example $("p").fadeOut("slow",function(){
238 * alert("Animation Done.");
243 * @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).
244 * @param Function callback (optional) A function to be executed whenever the animation completes.
246 * @see fadeIn(String|Number,Function)
247 * @see fadeTo(String|Number,Number,Function)
249 fadeOut: function(speed, callback){
250 return this.animate({opacity: "hide"}, speed, callback);
254 * Fade the opacity of all matched elements to a specified opacity and firing an
255 * optional callback after completion.
257 * Only the opacity is adjusted for this animation, meaning that
258 * all of the matched elements should already have some form of height
259 * and width associated with them.
261 * @example $("p").fadeTo("slow", 0.5);
263 * @example $("p").fadeTo("slow", 0.5, function(){
264 * alert("Animation Done.");
269 * @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).
270 * @param Number opacity The opacity to fade to (a number from 0 to 1).
271 * @param Function callback (optional) A function to be executed whenever the animation completes.
273 * @see fadeIn(String|Number,Function)
274 * @see fadeOut(String|Number,Function)
276 fadeTo: function(speed,to,callback){
277 return this.animate({opacity: to}, speed, callback);
281 * A function for making your own, custom, animations. The key aspect of
282 * this function is the object of style properties that will be animated,
283 * and to what end. Each key within the object represents a style property
284 * that will also be animated (for example: "height", "top", or "opacity").
286 * The value associated with the key represents to what end the property
287 * will be animated. If a number is provided as the value, then the style
288 * property will be transitioned from its current state to that new number.
289 * Oterwise if the string "hide", "show", or "toggle" is provided, a default
290 * animation will be constructed for that property.
292 * @example $("p").animate({
293 * height: 'toggle', opacity: 'toggle'
296 * @example $("p").animate({
297 * left: 50, opacity: 'show'
300 * @example $("p").animate({
302 * }, "slow", "easein");
303 * @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).
307 * @param Hash params A set of style attributes that you wish to animate, and to what end.
308 * @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).
309 * @param String easing (optional) The name of the easing effect that you want to use (Plugin Required).
310 * @param Function callback (optional) A function to be executed whenever the animation completes.
313 animate: function( prop, speed, easing, callback ) {
314 return this.queue(function(){
316 this.curAnim = jQuery.extend({}, prop);
317 var opt = jQuery.speed(speed, easing, callback);
319 for ( var p in prop ) {
320 var e = new jQuery.fx( this, opt, p );
321 if ( prop[p].constructor == Number )
322 e.custom( e.cur(), prop[p] );
324 e[ prop[p] ]( prop );
334 queue: function(type,fn){
340 return this.each(function(){
344 if ( !this.queue[type] )
345 this.queue[type] = [];
347 this.queue[type].push( fn );
349 if ( this.queue[type].length == 1 )
358 speed: function(speed, easing, fn) {
359 var opt = speed && speed.constructor == Object ? speed : {
360 complete: fn || !fn && easing ||
361 speed && speed.constructor == Function && speed,
363 easing: fn && easing || easing && easing.constructor != Function && easing
366 opt.duration = (opt.duration && opt.duration.constructor == Number ?
368 { slow: 600, fast: 200 }[opt.duration]) || 400;
371 opt.oldComplete = opt.complete;
372 opt.complete = function(){
373 jQuery.dequeue(this, "fx");
374 if ( opt.oldComplete && opt.oldComplete.constructor == Function )
375 opt.oldComplete.apply( this );
385 dequeue: function(elem,type){
388 if ( elem.queue && elem.queue[type] ) {
390 elem.queue[type].shift();
393 var f = elem.queue[type][0];
395 if ( f ) f.apply( elem );
400 * I originally wrote fx() as a clone of moo.fx and in the process
401 * of making it small in size the code became illegible to sane
402 * people. You've been warned.
405 fx: function( elem, options, prop ){
412 // Store display property
413 var oldDisplay = jQuery.css(elem, 'display');
414 // Set display property to block for animation
416 // Make sure that nothing sneaks out
417 y.overflow = "hidden";
419 // Simple function for setting a style value
422 options.step.apply( elem, [ z.now ] );
424 if ( prop == "opacity" )
425 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
426 else if ( parseInt(z.now) ) // My hate for IE will never die
427 y[prop] = parseInt(z.now) + "px";
430 // Figure out the maximum number to run to
432 return parseFloat( jQuery.css(elem,prop) );
435 // Get the current size
437 var r = parseFloat( jQuery.curCSS(elem, prop) );
438 return r && r > -10000 ? r : z.max();
441 // Start an animation from one number to another
442 z.custom = function(from,to){
443 z.startTime = (new Date()).getTime();
447 z.timer = setInterval(function(){
452 // Simple 'show' function
454 if ( !elem.orig ) elem.orig = {};
456 // Remember where we started, so that we can go back to it later
457 elem.orig[prop] = this.cur();
461 // Begin the animation
462 z.custom(0, elem.orig[prop]);
464 // Stupid IE, look what you made me do
465 if ( prop != "opacity" )
469 // Simple 'hide' function
471 if ( !elem.orig ) elem.orig = {};
473 // Remember where we started, so that we can go back to it later
474 elem.orig[prop] = this.cur();
478 // Begin the animation
479 z.custom(elem.orig[prop], 0);
482 //Simple 'toggle' function
483 z.toggle = function() {
484 if ( !elem.orig ) elem.orig = {};
486 // Remember where we started, so that we can go back to it later
487 elem.orig[prop] = this.cur();
489 if(oldDisplay == 'none') {
492 // Stupid IE, look what you made me do
493 if ( prop != "opacity" )
496 // Begin the animation
497 z.custom(0, elem.orig[prop]);
501 // Begin the animation
502 z.custom(elem.orig[prop], 0);
506 // Each step of an animation
507 z.step = function(firstNum, lastNum){
508 var t = (new Date()).getTime();
510 if (t > options.duration + z.startTime) {
512 clearInterval(z.timer);
518 if (elem.curAnim) elem.curAnim[ prop ] = true;
521 for ( var i in elem.curAnim )
522 if ( elem.curAnim[i] !== true )
526 // Reset the overflow
530 y.display = oldDisplay;
531 if (jQuery.css(elem, 'display') == 'none')
534 // Hide the element if the "hide" operation was done
538 // Reset the properties, if the item has been hidden or shown
539 if ( options.hide || options.show )
540 for ( var p in elem.curAnim )
542 jQuery.attr(y, p, elem.orig[p]);
547 // If a callback was provided, execute it
548 if ( done && options.complete && options.complete.constructor == Function )
549 // Execute the complete function
550 options.complete.apply( elem );
552 var n = t - this.startTime;
553 // Figure out where in the animation we are and set the number
554 var p = n / options.duration;
556 // If the easing function exists, then use it
557 z.now = options.easing && jQuery.easing[options.easing] ?
558 jQuery.easing[options.easing](p, n, firstNum, (lastNum-firstNum), options.duration) :
559 // else use default linear easing
560 ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
562 // Perform the next step of the animation