Added enchancement for #1994 by adding two parameters to .stop() which give additiona...
[jquery.git] / src / fx.js
index a143df6..b35dbcd 100644 (file)
--- a/src/fx.js
+++ b/src/fx.js
@@ -66,10 +66,10 @@ jQuery.fn.extend({
        },
        
        animate: function( prop, speed, easing, callback ) {
-               var opt = jQuery.speed(speed, easing, callback);
+               var optall = jQuery.speed(speed, easing, callback);
 
-               return this[ opt.queue === false ? "each" : "queue" ](function(){
-                       opt = jQuery.extend({}, opt);
+               return this[ optall.queue === false ? "each" : "queue" ](function(){
+                       var opt = jQuery.extend({}, optall);
                        var hidden = jQuery(this).is(":hidden"), self = this;
                        
                        for ( var p in prop ) {
@@ -126,7 +126,7 @@ jQuery.fn.extend({
        },
        
        queue: function(type, fn){
-               if ( jQuery.isFunction(type) ) {
+               if ( jQuery.isFunction(type) || ( type && type.constructor == Array )) {
                        fn = type;
                        type = "fx";
                }
@@ -146,14 +146,28 @@ jQuery.fn.extend({
                });
        },
 
-       stop: function(){
+       stop: function(clearQueue, gotoEnd){
                var timers = jQuery.timers;
 
-               return this.each(function(){
-                       for ( var i = 0; i < timers.length; i++ )
-                               if ( timers[i].elem == this )
-                                       timers.splice(i--, 1);
-               }).dequeue();
+               if (clearQueue)
+                       this.queue([]);
+
+               this.each(function(){
+                       // go in reverse order so anything added to the queue during the loop is ignored
+                       for ( var i = timers.length - 1; i >= 0; i-- )
+                               if ( timers[i].elem == this ) {
+                                       if (gotoEnd)
+                                               // force the next step to be the last
+                                               timers[i](true);
+                                       timers.splice(i, 1);
+                               }
+               });
+
+               // start the next in the queue if the last step wasn't forced
+               if (!gotoEnd)
+                       this.dequeue();
+
+               return this;
        }
 
 });
@@ -162,6 +176,8 @@ var queue = function( elem, type, array ) {
        if ( !elem )
                return;
 
+       type = type || "fx";
+
        var q = jQuery.data( elem, type + "queue" );
 
        if ( !q || array )
@@ -201,7 +217,8 @@ jQuery.extend({
                // Queueing
                opt.old = opt.complete;
                opt.complete = function(){
-                       jQuery(this).dequeue();
+                       if ( opt.queue !== false )
+                               jQuery(this).dequeue();
                        if ( jQuery.isFunction( opt.old ) )
                                opt.old.apply( this );
                };
@@ -219,6 +236,7 @@ jQuery.extend({
        },
        
        timers: [],
+       timerId: null,
 
        fx: function( elem, options, prop ){
                this.options = options;
@@ -250,8 +268,8 @@ jQuery.fx.prototype = {
                if ( this.elem[this.prop] != null && this.elem.style[this.prop] == null )
                        return this.elem[ this.prop ];
 
-               var r = parseFloat(jQuery.curCSS(this.elem, this.prop, force));
-               return r && r > -10000 ? r : parseFloat(jQuery.css(this.elem, this.prop)) || 0;
+               var r = parseFloat(jQuery.css(this.elem, this.prop, force));
+               return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
        },
 
        // Start an animation from one number to another
@@ -265,24 +283,26 @@ jQuery.fx.prototype = {
                this.update();
 
                var self = this;
-               function t(){
-                       return self.step();
+               function t(gotoEnd){
+                       return self.step(gotoEnd);
                }
 
                t.elem = this.elem;
 
                jQuery.timers.push(t);
 
-               if ( jQuery.timers.length == 1 ) {
-                       var timer = setInterval(function(){
+               if ( jQuery.timerId == null ) {
+                       jQuery.timerId = setInterval(function(){
                                var timers = jQuery.timers;
                                
                                for ( var i = 0; i < timers.length; i++ )
                                        if ( !timers[i]() )
                                                timers.splice(i--, 1);
 
-                               if ( !timers.length )
-                                       clearInterval( timer );
+                               if ( !timers.length ) {
+                                       clearInterval( jQuery.timerId );
+                                       jQuery.timerId = null;
+                               }
                        }, 13);
                }
        },
@@ -316,10 +336,10 @@ jQuery.fx.prototype = {
        },
 
        // Each step of an animation
-       step: function(){
+       step: function(gotoEnd){
                var t = (new Date()).getTime();
 
-               if ( t > this.options.duration + this.startTime ) {
+               if ( gotoEnd || t > this.options.duration + this.startTime ) {
                        this.now = this.end;
                        this.pos = this.state = 1;
                        this.update();