Testsuite 2.0
[jquery.git] / src / fx / fx.js
index c9f078e..7c2218e 100644 (file)
@@ -142,10 +142,39 @@ jQuery.fn.extend({
                return this.animate({height: "hide"}, speed, callback);
        },
 
+       /**
+        * Toggle the visibility of all matched elements by adjusting their height.
+        * Only the height is adjusted for this animation, causing all matched
+        * elements to be hidden in a "sliding" manner.
+        *
+        * @example $("p").slideToggle("slow");
+        *
+        * @name slideToggle
+        * @type jQuery
+        * @param Object 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).
+        * @cat Effects/Animations
+        */
+        
+       /**
+        * Toggle the visibility of all matched elements by adjusting their height
+        * and firing a callback function after completion.
+        * Only the height is adjusted for this animation, causing all matched
+        * elements to be hidden in a "sliding" manner.
+        *
+        * @example $("p").slideToggle("slow",function(){
+        *   alert("Animation Done.");
+        * });
+        *
+        * @name slideToggle
+        * @type jQuery
+        * @param Object 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).
+        * @param Function callback A function to be executed whenever the animation completes.
+        * @cat Effects/Animations
+        */
        slideToggle: function(speed,callback){
                return this.each(function(){
-                       var state = $(this).is(":hidden") ? "show" : "hide";
-                       $(this).animate({height: state}, speed, callback);
+                       var state = jQuery(this).is(":hidden") ? "show" : "hide";
+                       jQuery(this).animate({height: state}, speed, callback);
                });
        },
        
@@ -257,12 +286,36 @@ jQuery.fn.extend({
        },
        
        /**
-        * @private
+        * A function for making your own, custom, animations. The key aspect of
+        * this function is the object of style properties that will be animated,
+        * and to what end. Each key within the object represents a style property
+        * that will also be animated (for example: "height", "top", or "opacity").
+        *
+        * The value associated with the key represents to what end the property
+        * will be animated. If a number is provided as the value, then the style
+        * property will be transitioned from its current state to that new number.
+        * Oterwise if the string "hide", "show", or "toggle" is provided, a default
+        * animation will be constructed for that property.
+        *
+        * @example $("p").animate({
+        *   height: 'toggle', opacity: 'toggle'
+        * }, "slow");
+        *
+        * @example $("p").animate({
+        *   left: 50, opacity: 'show'
+        * }, 500);
+        *
+        * @name animate
+        * @type jQuery
+        * @param Hash params A set of style attributes that you wish to animate, and to what end.
+        * @param Object 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).
+        * @param Function callback A function to be executed whenever the animation completes.
+        * @cat Effects/Animations
         */
        animate: function(prop,speed,callback) {
                return this.queue(function(){
                
-                       this.curAnim = prop;
+                       this.curAnim = jQuery.extend({}, prop);
                        
                        for ( var p in prop ) {
                                var e = new jQuery.fx( this, jQuery.speed(speed,callback), p );
@@ -302,34 +355,6 @@ jQuery.fn.extend({
 });
 
 jQuery.extend({
-
-       setAuto: function(e,p) {
-               if ( e.notAuto ) return;
-
-               if ( p == "height" && e.scrollHeight != parseInt(jQuery.curCSS(e,p)) ) return;
-               if ( p == "width" && e.scrollWidth != parseInt(jQuery.curCSS(e,p)) ) return;
-
-               // Remember the original height
-               var a = e.style[p];
-
-               // Figure out the size of the height right now
-               var o = jQuery.curCSS(e,p,1);
-
-               if ( p == "height" && e.scrollHeight != o ||
-                       p == "width" && e.scrollWidth != o ) return;
-
-               // Set the height to auto
-               e.style[p] = e.currentStyle ? "" : "auto";
-
-               // See what the size of "auto" is
-               var n = jQuery.curCSS(e,p,1);
-
-               // Revert back to the original size
-               if ( o != n && n != "auto" ) {
-                       e.style[p] = a;
-                       e.notAuto = true;
-               }
-       },
        
        speed: function(s,o) {
                o = o || {};
@@ -374,77 +399,79 @@ jQuery.extend({
         */
        
        fx: function( elem, options, prop ){
-       
+
                var z = this;
-       
+
                // The users options
                z.o = {
                        duration: options.duration || 400,
                        complete: options.complete,
                        step: options.step
                };
-       
+
                // The element
                z.el = elem;
-       
+
                // The styles
                var y = z.el.style;
-       
+               
+               // Store display property
+               var oldDisplay = jQuery.css(z.el, 'display');
+               // Set display property to block for animation
+               y.display = "block";
+               // Make sure that nothing sneaks out
+               y.overflow = "hidden";
+
                // Simple function for setting a style value
                z.a = function(){
                        if ( options.step )
                                options.step.apply( elem, [ z.now ] );
 
-                       if ( prop == "opacity" ) {
-                               if (z.now == 1) z.now = 0.9999;
-                               if (window.ActiveXObject)
-                                       y.filter = "alpha(opacity=" + z.now*100 + ")";
-                               else
-                                       y.opacity = z.now;
-
-                       // My hate for IE will never die
-                       } else if ( parseInt(z.now) )
+                       if ( prop == "opacity" )
+                               jQuery.attr(y, "opacity", z.now); // Let attr handle opacity
+                       else if ( parseInt(z.now) ) // My hate for IE will never die
                                y[prop] = parseInt(z.now) + "px";
-                               
-                       y.display = "block";
                };
-       
+
                // Figure out the maximum number to run to
                z.max = function(){
                        return parseFloat( jQuery.css(z.el,prop) );
                };
-       
+
                // Get the current size
                z.cur = function(){
                        var r = parseFloat( jQuery.curCSS(z.el, prop) );
                        return r && r > -10000 ? r : z.max();
                };
-       
+
                // Start an animation from one number to another
                z.custom = function(from,to){
                        z.startTime = (new Date()).getTime();
                        z.now = from;
                        z.a();
-       
+
                        z.timer = setInterval(function(){
                                z.step(from, to);
                        }, 13);
                };
-       
+
                // Simple 'show' function
-               z.show = function( p ){
+               z.show = function(){
                        if ( !z.el.orig ) z.el.orig = {};
 
                        // Remember where we started, so that we can go back to it later
                        z.el.orig[prop] = this.cur();
 
-                       z.custom( 0, z.el.orig[prop] );
+                       z.o.show = true;
+
+                       // Begin the animation
+                       z.custom(0, z.el.orig[prop]);
 
                        // Stupid IE, look what you made me do
                        if ( prop != "opacity" )
                                y[prop] = "1px";
                };
-       
+
                // Simple 'hide' function
                z.hide = function(){
                        if ( !z.el.orig ) z.el.orig = {};
@@ -457,22 +484,11 @@ jQuery.extend({
                        // Begin the animation
                        z.custom(z.el.orig[prop], 0);
                };
-       
-               // IE has trouble with opacity if it does not have layout
-               if ( jQuery.browser.msie && !z.el.currentStyle.hasLayout )
-                       y.zoom = "1";
-       
-               // Remember  the overflow of the element
-               if ( !z.el.oldOverlay )
-                       z.el.oldOverflow = jQuery.css( z.el, "overflow" );
-       
-               // Make sure that nothing sneaks out
-               y.overflow = "hidden";
-       
+
                // Each step of an animation
                z.step = function(firstNum, lastNum){
                        var t = (new Date()).getTime();
-       
+
                        if (t > z.o.duration + z.startTime) {
                                // Stop the timer
                                clearInterval(z.timer);
@@ -482,30 +498,32 @@ jQuery.extend({
                                z.a();
 
                                z.el.curAnim[ prop ] = true;
-                               
+
                                var done = true;
                                for ( var i in z.el.curAnim )
                                        if ( z.el.curAnim[i] !== true )
                                                done = false;
-                                               
+
                                if ( done ) {
                                        // Reset the overflow
-                                       y.overflow = z.el.oldOverflow;
-                               
+                                       y.overflow = '';
+                                       
+                                       // Reset the display
+                                       y.display = oldDisplay;
+                                       if (jQuery.css(z.el, 'display') == 'none')
+                                               y.display = 'block';
+
                                        // Hide the element if the "hide" operation was done
                                        if ( z.o.hide ) 
                                                y.display = 'none';
-                                       
-                                       // Reset the property, if the item has been hidden
-                                       if ( z.o.hide ) {
-                                               for ( var p in z.el.curAnim ) {
-                                                       y[ p ] = z.el.orig[p] + ( p == "opacity" ? "" : "px" );
-       
-                                                       // set its height and/or width to auto
-                                                       if ( p == 'height' || p == 'width' )
-                                                               jQuery.setAuto( z.el, p );
-                                               }
-                                       }
+
+                                       // Reset the properties, if the item has been hidden or shown
+                                       if ( z.o.hide || z.o.show )
+                                               for ( var p in z.el.curAnim )
+                                                       if (p == "opacity")
+                                                               jQuery.attr(y, p, z.el.orig[p]);
+                                                       else
+                                                               y[p] = '';
                                }
 
                                // If a callback was provided, execute it
@@ -516,7 +534,7 @@ jQuery.extend({
                                // Figure out where in the animation we are and set the number
                                var p = (t - this.startTime) / z.o.duration;
                                z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;
-       
+
                                // Perform the next step of the animation
                                z.a();
                        }