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){
38 height: "show", width: "show", opacity: "show"
41 this.filter(":hidden").each(function(){
42 this.style.display = this.oldblock ? this.oldblock : "";
43 if ( jQuery.css(this,"display") == "none" )
44 this.style.display = "block";
49 * Hides each of the set of matched elements if they are shown.
51 * @example $("p").hide()
52 * @before <p>Hello</p>
53 * @result [ <p style="display: none">Hello</p> ]
61 * Hide all matched elements using a graceful animation and firing an
62 * optional callback after completion.
64 * The height, width, and opacity of each of the matched elements
65 * are changed dynamically according to the specified speed.
67 * @example $("p").hide("slow");
69 * @example $("p").hide("slow",function(){
70 * alert("Animation Done.");
75 * @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).
76 * @param Function callback (optional) A function to be executed whenever the animation completes.
78 * @see show(String|Number,Function)
80 hide: function(speed,callback){
83 height: "hide", width: "hide", opacity: "hide"
86 this.filter(":visible").each(function(){
87 this.oldblock = this.oldblock || jQuery.css(this,"display");
88 if ( this.oldblock == "none" )
89 this.oldblock = "block";
90 this.style.display = "none";
94 // Save the old toggle function
95 _toggle: jQuery.fn.toggle,
98 * Toggles each of the set of matched elements. If they are shown,
99 * toggle makes them hidden. If they are hidden, toggle
102 * @example $("p").toggle()
103 * @before <p>Hello</p><p style="display: none">Hello Again</p>
104 * @result [ <p style="display: none">Hello</p>, <p style="display: block">Hello Again</p> ]
110 toggle: function( fn, fn2 ){
111 return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
112 this._toggle( fn, fn2 ) :
114 height: "toggle", width: "toggle", opacity: "toggle"
119 * Reveal all matched elements by adjusting their height and firing an
120 * optional callback after completion.
122 * Only the height is adjusted for this animation, causing all matched
123 * elements to be revealed in a "sliding" manner.
125 * @example $("p").slideDown("slow");
127 * @example $("p").slideDown("slow",function(){
128 * alert("Animation Done.");
133 * @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).
134 * @param Function callback (optional) A function to be executed whenever the animation completes.
136 * @see slideUp(String|Number,Function)
137 * @see slideToggle(String|Number,Function)
139 slideDown: function(speed,callback){
140 return this.animate({height: "show"}, speed, callback);
144 * Hide all matched elements by adjusting their height and firing an
145 * optional callback after completion.
147 * Only the height is adjusted for this animation, causing all matched
148 * elements to be hidden in a "sliding" manner.
150 * @example $("p").slideUp("slow");
152 * @example $("p").slideUp("slow",function(){
153 * alert("Animation Done.");
158 * @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).
159 * @param Function callback (optional) A function to be executed whenever the animation completes.
161 * @see slideDown(String|Number,Function)
162 * @see slideToggle(String|Number,Function)
164 slideUp: function(speed,callback){
165 return this.animate({height: "hide"}, speed, callback);
169 * Toggle the visibility of all matched elements by adjusting their height and firing an
170 * optional callback after completion.
172 * Only the height is adjusted for this animation, causing all matched
173 * elements to be hidden in a "sliding" manner.
175 * @example $("p").slideToggle("slow");
177 * @example $("p").slideToggle("slow",function(){
178 * alert("Animation Done.");
183 * @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).
184 * @param Function callback (optional) A function to be executed whenever the animation completes.
186 * @see slideDown(String|Number,Function)
187 * @see slideUp(String|Number,Function)
189 slideToggle: function(speed, callback){
190 return this.animate({height: "toggle"}, speed, callback);
194 * Fade in all matched elements by adjusting their opacity and firing an
195 * optional callback after completion.
197 * Only the opacity is adjusted for this animation, meaning that
198 * all of the matched elements should already have some form of height
199 * and width associated with them.
201 * @example $("p").fadeIn("slow");
203 * @example $("p").fadeIn("slow",function(){
204 * alert("Animation Done.");
209 * @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).
210 * @param Function callback (optional) A function to be executed whenever the animation completes.
212 * @see fadeOut(String|Number,Function)
213 * @see fadeTo(String|Number,Number,Function)
215 fadeIn: function(speed, callback){
216 return this.animate({opacity: "show"}, speed, callback);
220 * Fade out all matched elements by adjusting their opacity and firing an
221 * optional callback after completion.
223 * Only the opacity is adjusted for this animation, meaning that
224 * all of the matched elements should already have some form of height
225 * and width associated with them.
227 * @example $("p").fadeOut("slow");
229 * @example $("p").fadeOut("slow",function(){
230 * alert("Animation Done.");
235 * @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).
236 * @param Function callback (optional) A function to be executed whenever the animation completes.
238 * @see fadeIn(String|Number,Function)
239 * @see fadeTo(String|Number,Number,Function)
241 fadeOut: function(speed, callback){
242 return this.animate({opacity: "hide"}, speed, callback);
246 * Fade the opacity of all matched elements to a specified opacity and firing an
247 * optional callback after completion.
249 * Only the opacity is adjusted for this animation, meaning that
250 * all of the matched elements should already have some form of height
251 * and width associated with them.
253 * @example $("p").fadeTo("slow", 0.5);
255 * @example $("p").fadeTo("slow", 0.5, function(){
256 * alert("Animation Done.");
261 * @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).
262 * @param Number opacity The opacity to fade to (a number from 0 to 1).
263 * @param Function callback (optional) A function to be executed whenever the animation completes.
265 * @see fadeIn(String|Number,Function)
266 * @see fadeOut(String|Number,Function)
268 fadeTo: function(speed,to,callback){
269 return this.animate({opacity: to}, speed, callback);
273 * A function for making your own, custom animations. The key aspect of
274 * this function is the object of style properties that will be animated,
275 * and to what end. Each key within the object represents a style property
276 * that will also be animated (for example: "height", "top", or "opacity").
278 * Note that properties should be specified using camel case
279 * eg. marginLeft instead of margin-left.
281 * The value associated with the key represents to what end the property
282 * will be animated. If a number is provided as the value, then the style
283 * property will be transitioned from its current state to that new number.
284 * Otherwise if the string "hide", "show", or "toggle" is provided, a default
285 * animation will be constructed for that property.
287 * @example $("p").animate({
288 * height: 'toggle', opacity: 'toggle'
291 * @example $("p").animate({
292 * left: 50, opacity: 'show'
295 * @example $("p").animate({
297 * }, "slow", "easein");
298 * @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).
302 * @param Hash params A set of style attributes that you wish to animate, and to what end.
303 * @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).
304 * @param String easing (optional) The name of the easing effect that you want to use (e.g. swing or linear). Defaults to "swing".
305 * @param Function callback (optional) A function to be executed whenever the animation completes.
308 animate: function( prop, speed, easing, callback ) {
309 return this.queue(function(){
310 var hidden = jQuery(this).is(":hidden");
312 for ( var p in prop )
313 if ( prop[p] == "hide" && hidden ||
314 prop[p] == "show" && !hidden )
317 this.curAnim = jQuery.extend({}, prop);
318 var opt = jQuery.speed(speed, easing, callback);
321 jQuery.each( prop, function(name, val){
322 var e = new jQuery.fx( self, opt, name );
323 if ( val.constructor == Number )
324 e.custom( e.cur(), val );
326 e[ val == "toggle" ? hidden ? "show" : "hide" : val ]( prop );
335 queue: function(type,fn){
341 return this.each(function(){
345 if ( !this.queue[type] )
346 this.queue[type] = [];
348 this.queue[type].push( fn );
350 if ( this.queue[type].length == 1 )
359 speed: function(speed, easing, fn) {
360 var opt = speed && speed.constructor == Object ? speed : {
361 complete: fn || !fn && easing ||
362 jQuery.isFunction( speed ) && speed,
364 easing: fn && easing || easing && easing.constructor != Function && easing || "swing"
367 opt.duration = (opt.duration && opt.duration.constructor == Number ?
369 { slow: 600, fast: 200 }[opt.duration]) || 400;
372 opt.old = opt.complete;
373 opt.complete = function(){
374 jQuery.dequeue(this, "fx");
375 if ( jQuery.isFunction( opt.old ) )
376 opt.old.apply( this );
383 linear: function( p, n, firstNum, diff ) {
384 return firstNum + diff * p;
386 swing: function( p, n, firstNum, diff ) {
387 return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
393 dequeue: function(elem,type){
396 if ( elem.queue && elem.queue[type] ) {
398 elem.queue[type].shift();
401 var f = elem.queue[type][0];
403 if ( f ) f.apply( elem );
410 * I originally wrote fx() as a clone of moo.fx and in the process
411 * of making it small in size the code became illegible to sane
412 * people. You've been warned.
415 fx: function( elem, options, prop ){
422 if ( prop == "height" || prop == "width" ) {
423 // Store display property
424 var oldDisplay = jQuery.css(elem, "display");
426 // Make sure that nothing sneaks out
427 var oldOverflow = y.overflow;
428 y.overflow = "hidden";
431 // Simple function for setting a style value
434 options.step.apply( elem, [ z.now ] );
436 if ( prop == "opacity" )
437 jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
439 y[prop] = parseInt(z.now) + "px";
440 y.display = "block"; // Set display property to block for animation
444 // Figure out the maximum number to run to
446 return parseFloat( jQuery.css(elem,prop) );
449 // Get the current size
451 var r = parseFloat( jQuery.curCSS(elem, prop) );
452 return r && r > -10000 ? r : z.max();
455 // Start an animation from one number to another
456 z.custom = function(from,to){
457 z.startTime = (new Date()).getTime();
461 jQuery.timers.push(function(){
462 return z.step(from, to);
465 if ( jQuery.timers.length == 1 ) {
466 var timer = setInterval(function(){
467 var timers = jQuery.timers;
469 for ( var i = 0; i < timers.length; i++ )
471 timers.splice(i--, 1);
473 if ( !timers.length )
474 clearInterval( timer );
479 // Simple 'show' function
481 if ( !elem.orig ) elem.orig = {};
483 // Remember where we started, so that we can go back to it later
484 elem.orig[prop] = jQuery.attr( elem.style, prop );
488 // Begin the animation
489 z.custom(0, this.cur());
491 // Make sure that we start at a small width/height to avoid any
493 if ( prop != "opacity" )
496 // Start by showing the element
500 // Simple 'hide' function
502 if ( !elem.orig ) elem.orig = {};
504 // Remember where we started, so that we can go back to it later
505 elem.orig[prop] = jQuery.attr( elem.style, prop );
509 // Begin the animation
510 z.custom(this.cur(), 0);
513 // Each step of an animation
514 z.step = function(firstNum, lastNum){
515 var t = (new Date()).getTime();
517 if (t > options.duration + z.startTime) {
521 if (elem.curAnim) elem.curAnim[ prop ] = true;
524 for ( var i in elem.curAnim )
525 if ( elem.curAnim[i] !== true )
529 if ( oldDisplay != null ) {
530 // Reset the overflow
531 y.overflow = oldOverflow;
534 y.display = oldDisplay;
535 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 )
546 jQuery.attr(y, p, elem.orig[p]);
549 // If a callback was provided, execute it
550 if ( done && jQuery.isFunction( options.complete ) )
551 // Execute the complete function
552 options.complete.apply( elem );
556 var n = t - this.startTime;
557 // Figure out where in the animation we are and set the number
558 var p = n / options.duration;
560 // Perform the easing function, defaults to swing
561 z.now = jQuery.easing[options.easing](p, n, firstNum, (lastNum-firstNum), options.duration);
563 // Perform the next step of the animation