Started cleaning up fx, made easing easier to use.
authorJohn Resig <jeresig@gmail.com>
Sun, 7 Jan 2007 00:28:31 +0000 (00:28 +0000)
committerJohn Resig <jeresig@gmail.com>
Sun, 7 Jan 2007 00:28:31 +0000 (00:28 +0000)
src/fx/fx.js

index f0b9c8e..bb3a17a 100644 (file)
@@ -24,10 +24,10 @@ jQuery.fn.extend({
         * @cat Effects/Animations\r
         * @see hide(String|Number,Function)\r
         */\r
-       show: function(speed,callback, easing){\r
+       show: function(speed,callback){\r
                return speed ? this.animate({\r
                        height: "show", width: "show", opacity: "show"\r
-               }, speed, callback, easing) : this._show();\r
+               }, speed, callback) : this._show();\r
        },\r
        \r
        // Overwrite the old hide method\r
@@ -54,10 +54,10 @@ jQuery.fn.extend({
         * @cat Effects/Animations\r
         * @see show(String|Number,Function)\r
         */\r
-       hide: function(speed,callback, easing){\r
+       hide: function(speed,callback){\r
                return speed ? this.animate({\r
                        height: "hide", width: "hide", opacity: "hide"\r
-               }, speed, callback, easing) : this._hide();\r
+               }, speed, callback) : this._hide();\r
        },\r
        \r
        /**\r
@@ -82,8 +82,8 @@ jQuery.fn.extend({
         * @see slideUp(String|Number,Function)\r
         * @see slideToggle(String|Number,Function)\r
         */\r
-       slideDown: function(speed,callback, easing){\r
-               return this.animate({height: "show"}, speed, callback, easing);\r
+       slideDown: function(speed,callback){\r
+               return this.animate({height: "show"}, speed, callback);\r
        },\r
        \r
        /**\r
@@ -108,8 +108,8 @@ jQuery.fn.extend({
         * @see slideDown(String|Number,Function)\r
         * @see slideToggle(String|Number,Function)\r
         */\r
-       slideUp: function(speed,callback, easing){\r
-               return this.animate({height: "hide"}, speed, callback, easing);\r
+       slideUp: function(speed,callback){\r
+               return this.animate({height: "hide"}, speed, callback);\r
        },\r
 \r
        /**\r
@@ -134,10 +134,10 @@ jQuery.fn.extend({
         * @see slideDown(String|Number,Function)\r
         * @see slideUp(String|Number,Function)\r
         */\r
-       slideToggle: function(speed, callback, easing){\r
+       slideToggle: function(speed, callback){\r
                return this.each(function(){\r
                        var state = jQuery(this).is(":hidden") ? "show" : "hide";\r
-                       jQuery(this).animate({height: state}, speed, callback, easing);\r
+                       jQuery(this).animate({height: state}, speed, callback);\r
                });\r
        },\r
        \r
@@ -164,8 +164,8 @@ jQuery.fn.extend({
         * @see fadeOut(String|Number,Function)\r
         * @see fadeTo(String|Number,Number,Function)\r
         */\r
-       fadeIn: function(speed, callback, easing){\r
-               return this.animate({opacity: "show"}, speed, callback, easing);\r
+       fadeIn: function(speed, callback){\r
+               return this.animate({opacity: "show"}, speed, callback);\r
        },\r
        \r
        /**\r
@@ -191,8 +191,8 @@ jQuery.fn.extend({
         * @see fadeIn(String|Number,Function)\r
         * @see fadeTo(String|Number,Number,Function)\r
         */\r
-       fadeOut: function(speed, callback, easing){\r
-               return this.animate({opacity: "hide"}, speed, callback, easing);\r
+       fadeOut: function(speed, callback){\r
+               return this.animate({opacity: "hide"}, speed, callback);\r
        },\r
        \r
        /**\r
@@ -219,8 +219,8 @@ jQuery.fn.extend({
         * @see fadeIn(String|Number,Function)\r
         * @see fadeOut(String|Number,Function)\r
         */\r
-       fadeTo: function(speed,to,callback, easing){\r
-               return this.animate({opacity: to}, speed, callback, easing);\r
+       fadeTo: function(speed,to,callback){\r
+               return this.animate({opacity: to}, speed, callback);\r
        },\r
        \r
        /**\r
@@ -251,13 +251,14 @@ jQuery.fn.extend({
         * @param String easing (optional) easing effect\r
         * @cat Effects/Animations\r
         */\r
-       animate: function(prop,speed,callback, easing) {\r
+       animate: function( prop, speed, easing, callback ) {\r
                return this.queue(function(){\r
                \r
-                       this.curAnim = jQuery.extend({}, prop);\r
+                       this.curAnim = jQuery.extend({}, prop);
+                       var opt = jQuery.speed(speed, easing, callback);\r
                        \r
                        for ( var p in prop ) {\r
-                               var e = new jQuery.fx( this, jQuery.speed(speed,callback), p, easing );\r
+                               var e = new jQuery.fx( this, opt, p );\r
                                if ( prop[p].constructor == Number )\r
                                        e.custom( e.cur(), prop[p] );\r
                                else\r
@@ -295,25 +296,30 @@ jQuery.fn.extend({
 \r
 jQuery.extend({\r
        \r
-       speed: function(s,o) {\r
-               o = o || {};\r
-               \r
-               if ( o.constructor == Function )\r
-                       o = { complete: o };\r
-               \r
-               var ss = { slow: 600, fast: 200 };\r
-               o.duration = (s && s.constructor == Number ? s : ss[s]) || 400;\r
+       speed: function(speed, easing, fn) {\r
+               var opt = speed.constructor == Object ? speed : {
+                       complete: fn || !fn && easing || 
+                               speed.constructor == Function && speed,
+                       duration: speed,
+                       easing: fn && easing || easing && easing.constructor != Function && easing
+               };
+\r
+               opt.duration = (opt.duration.constructor == Number ? 
+                       opt.duration : 
+                       { slow: 600, fast: 200 }[opt.duration]) || 400;\r
        \r
                // Queueing\r
-               o.oldComplete = o.complete;\r
-               o.complete = function(){\r
+               opt.oldComplete = opt.complete;\r
+               opt.complete = function(){\r
                        jQuery.dequeue(this, "fx");\r
-                       if ( o.oldComplete && o.oldComplete.constructor == Function )\r
-                               o.oldComplete.apply( this );\r
+                       if ( opt.oldComplete && opt.oldComplete.constructor == Function )\r
+                               opt.oldComplete.apply( this );\r
                };\r
        \r
-               return o;\r
-       },\r
+               return opt;\r
+       },
+       
+       easing: {},\r
        \r
        queue: {},\r
        \r
@@ -337,26 +343,15 @@ jQuery.extend({
         * people. You've been warned.\r
         */\r
        \r
-       fx: function( elem, options, prop, easing ){\r
+       fx: function( elem, options, prop ){\r
 \r
                var z = this;\r
 \r
-               // The users options\r
-               z.o = {\r
-                       duration: options.duration || 400,\r
-                       complete: options.complete,\r
-                       step: options.step,\r
-                       easing : easing || 'linear'\r
-               };\r
-               \r
-               // The element\r
-               z.el = elem;\r
-\r
                // The styles\r
-               var y = z.el.style;\r
+               var y = elem.style;\r
                \r
                // Store display property\r
-               var oldDisplay = jQuery.css(z.el, 'display');\r
+               var oldDisplay = jQuery.css(elem, 'display');\r
                // Set display property to block for animation\r
                y.display = "block";\r
                // Make sure that nothing sneaks out\r
@@ -375,12 +370,12 @@ jQuery.extend({
 \r
                // Figure out the maximum number to run to\r
                z.max = function(){\r
-                       return parseFloat( jQuery.css(z.el,prop) );\r
+                       return parseFloat( jQuery.css(elem,prop) );\r
                };\r
 \r
                // Get the current size\r
                z.cur = function(){\r
-                       var r = parseFloat( jQuery.curCSS(z.el, prop) );\r
+                       var r = parseFloat( jQuery.curCSS(elem, prop) );\r
                        return r && r > -10000 ? r : z.max();\r
                };\r
 \r
@@ -397,15 +392,15 @@ jQuery.extend({
 \r
                // Simple 'show' function\r
                z.show = function(){\r
-                       if ( !z.el.orig ) z.el.orig = {};\r
+                       if ( !elem.orig ) elem.orig = {};\r
 \r
                        // Remember where we started, so that we can go back to it later\r
-                       z.el.orig[prop] = this.cur();\r
+                       elem.orig[prop] = this.cur();\r
 \r
-                       z.o.show = true;\r
+                       options.show = true;\r
 \r
                        // Begin the animation\r
-                       z.custom(0, z.el.orig[prop]);\r
+                       z.custom(0, elem.orig[prop]);\r
 \r
                        // Stupid IE, look what you made me do\r
                        if ( prop != "opacity" )\r
@@ -414,38 +409,38 @@ jQuery.extend({
 \r
                // Simple 'hide' function\r
                z.hide = function(){\r
-                       if ( !z.el.orig ) z.el.orig = {};\r
+                       if ( !elem.orig ) elem.orig = {};\r
 \r
                        // Remember where we started, so that we can go back to it later\r
-                       z.el.orig[prop] = this.cur();\r
+                       elem.orig[prop] = this.cur();\r
 \r
-                       z.o.hide = true;\r
+                       options.hide = true;\r
 \r
                        // Begin the animation\r
-                       z.custom(z.el.orig[prop], 0);\r
+                       z.custom(elem.orig[prop], 0);\r
                };\r
                \r
                //Simple 'toggle' function\r
                z.toggle = function() {\r
-                       if ( !z.el.orig ) z.el.orig = {};\r
+                       if ( !elem.orig ) elem.orig = {};\r
 \r
                        // Remember where we started, so that we can go back to it later\r
-                       z.el.orig[prop] = this.cur();\r
+                       elem.orig[prop] = this.cur();\r
 \r
                        if(oldDisplay == 'none')  {\r
-                               z.o.show = true;\r
+                               options.show = true;\r
                                \r
                                // Stupid IE, look what you made me do\r
                                if ( prop != "opacity" )\r
                                        y[prop] = "1px";\r
 \r
                                // Begin the animation\r
-                               z.custom(0, z.el.orig[prop]);   \r
+                               z.custom(0, elem.orig[prop]);   \r
                        } else {\r
-                               z.o.hide = true;\r
+                               options.hide = true;\r
 \r
                                // Begin the animation\r
-                               z.custom(z.el.orig[prop], 0);\r
+                               z.custom(elem.orig[prop], 0);\r
                        }               \r
                };\r
 \r
@@ -453,7 +448,7 @@ jQuery.extend({
                z.step = function(firstNum, lastNum){\r
                        var t = (new Date()).getTime();\r
 \r
-                       if (t > z.o.duration + z.startTime) {\r
+                       if (t > options.duration + z.startTime) {\r
                                // Stop the timer\r
                                clearInterval(z.timer);\r
                                z.timer = null;\r
@@ -461,11 +456,11 @@ jQuery.extend({
                                z.now = lastNum;\r
                                z.a();\r
 \r
-                               if (z.el.curAnim) z.el.curAnim[ prop ] = true;\r
+                               if (elem.curAnim) elem.curAnim[ prop ] = true;\r
 \r
                                var done = true;\r
-                               for ( var i in z.el.curAnim )\r
-                                       if ( z.el.curAnim[i] !== true )\r
+                               for ( var i in elem.curAnim )\r
+                                       if ( elem.curAnim[i] !== true )\r
                                                done = false;\r
 \r
                                if ( done ) {\r
@@ -474,35 +469,36 @@ jQuery.extend({
                                        \r
                                        // Reset the display\r
                                        y.display = oldDisplay;\r
-                                       if (jQuery.css(z.el, 'display') == 'none')\r
+                                       if (jQuery.css(elem, 'display') == 'none')\r
                                                y.display = 'block';\r
 \r
                                        // Hide the element if the "hide" operation was done\r
-                                       if ( z.o.hide ) \r
+                                       if ( options.hide ) \r
                                                y.display = 'none';\r
 \r
                                        // Reset the properties, if the item has been hidden or shown\r
-                                       if ( z.o.hide || z.o.show )\r
-                                               for ( var p in z.el.curAnim )\r
+                                       if ( options.hide || options.show )\r
+                                               for ( var p in elem.curAnim )\r
                                                        if (p == "opacity")\r
-                                                               jQuery.attr(y, p, z.el.orig[p]);\r
+                                                               jQuery.attr(y, p, elem.orig[p]);\r
                                                        else\r
                                                                y[p] = '';\r
                                }\r
 \r
                                // If a callback was provided, execute it\r
-                               if( done && z.o.complete && z.o.complete.constructor == Function )\r
+                               if ( done && options.complete && options.complete.constructor == Function )\r
                                        // Execute the complete function\r
-                                       z.o.complete.apply( z.el );\r
+                                       options.complete.apply( elem );\r
                        } else {\r
                                var n = t - this.startTime;\r
                                // Figure out where in the animation we are and set the number\r
-                               var p = n / z.o.duration;\r
-                               //if the easing function exists then use it else use default linear easing\r
-                               if (jQuery.easing && jQuery.easing[z.o.easing])\r
-                                       z.now = jQuery.easing[z.o.easing](p, n,  firstNum, (lastNum-firstNum), z.o.duration);\r
-                               else \r
-                                       z.now = ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;\r
+                               var p = n / options.duration;
+                               \r
+                               // If the easing function exists, then use it \r
+                               z.now = options.easing && jQuery.easing[options.easing] ?
+                                       jQuery.easing[options.easing](p, n,  firstNum, (lastNum-firstNum), options.duration) :
+                                       // else use default linear easing
+                                       ((-Math.cos(p*Math.PI)/2) + 0.5) * (lastNum-firstNum) + firstNum;\r
 \r
                                // Perform the next step of the animation\r
                                z.a();\r