diff options
author | Corey Frang <gnarf@gnarf.net> | 2013-01-08 01:16:50 +0000 |
---|---|---|
committer | Dave Methvin <dave.methvin@gmail.com> | 2013-01-08 01:19:25 +0000 |
commit | b6abb31df4d5be80dc13844d9988fb0c990ae5ae (patch) | |
tree | 1f6171c02ef1278076d09d27ed5e8d6b5c79987e /src/effects.js | |
parent | 62acda819f9b6fba9263d0b613e15285807b23a7 (diff) | |
download | jquery-b6abb31df4d5be80dc13844d9988fb0c990ae5ae.tar.gz jquery-b6abb31df4d5be80dc13844d9988fb0c990ae5ae.zip |
Fix #13103. Add .finish() method. Close gh-1118.
Diffstat (limited to 'src/effects.js')
-rw-r--r-- | src/effects.js | 59 |
1 files changed, 55 insertions, 4 deletions
diff --git a/src/effects.js b/src/effects.js index f4f435fd2..7c73a920b 100644 --- a/src/effects.js +++ b/src/effects.js @@ -72,6 +72,7 @@ function createTweens( animation, props ) { function Animation( elem, properties, options ) { var result, + stopped, index = 0, length = animationPrefilters.length, deferred = jQuery.Deferred().always( function() { @@ -79,6 +80,9 @@ function Animation( elem, properties, options ) { delete tick.elem; }), tick = function() { + if ( stopped ) { + return false; + } var currentTime = fxNow || createFxNow(), remaining = Math.max( 0, animation.startTime + animation.duration - currentTime ), // archaic crash bug won't allow us to use 1 - ( 0.5 || 0 ) (#12497) @@ -120,7 +124,10 @@ function Animation( elem, properties, options ) { // if we are going to the end, we want to run all the tweens // otherwise we skip this part length = gotoEnd ? animation.tweens.length : 0; - + if ( stopped ) { + return this; + } + stopped = true; for ( ; index < length ; index++ ) { animation.tweens[ index ].run( 1 ); } @@ -468,12 +475,15 @@ jQuery.fn.extend({ doAnimation = function() { // Operate on a copy of prop so per-property easing won't be lost var anim = Animation( this, jQuery.extend( {}, prop ), optall ); - - // Empty animations resolve immediately - if ( empty ) { + doAnimation.finish = function() { + anim.stop( true ); + }; + // Empty animations, or finishing resolves immediately + if ( empty || jQuery._data( this, "finish" ) ) { anim.stop( true ); } }; + doAnimation.finish = doAnimation; return empty || optall.queue === false ? this.each( doAnimation ) : @@ -528,6 +538,47 @@ jQuery.fn.extend({ jQuery.dequeue( this, type ); } }); + }, + finish: function( type ) { + if ( type !== false ) { + type = type || "fx"; + } + return this.each(function() { + var index, + data = jQuery._data( this ), + queue = data[ type + "queue" ], + hooks = data[ type + "queueHooks" ], + timers = jQuery.timers, + length = queue ? queue.length : 0; + + // enable finishing flag on private data + data.finish = true; + + // empty the queue first + jQuery.queue( this, type, [] ); + + if ( hooks && hooks.cur && hooks.cur.finish ) { + hooks.cur.finish.call( this ); + } + + // look for any active animations, and finish them + for ( index = timers.length; index--; ) { + if ( timers[ index ].elem === this && timers[ index ].queue === type ) { + timers[ index ].anim.stop( true ); + timers.splice( index, 1 ); + } + } + + // look for any animations in the old queue and finish them + for ( index = 0; index < length; index++ ) { + if ( queue[ index ] && queue[ index ].finish ) { + queue[ index ].finish.call( this ); + } + } + + // turn off finishing flag + delete data.finish; + }); } }); |