jQuery API

.stop()

.stop( [clearQueue] [, jumpToEnd] ) Returns: jQuery

Description: Stop the currently-running animation on the matched elements.

  • version added: 1.2.stop( [clearQueue] [, jumpToEnd] )

    clearQueueA Boolean indicating whether to remove queued animation as well. Defaults to false.

    jumpToEndA Boolean indicating whether to complete the current animation immediately. Defaults to false.

  • version added: 1.7.stop( [queue] [, clearQueue] [, jumpToEnd] )

    queueThe name of the queue in which to stop animations.

    clearQueueA Boolean indicating whether to remove queued animation as well. Defaults to false.

    jumpToEndA Boolean indicating whether to complete the current animation immediately. Defaults to false.

When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. If, for instance, an element is being hidden with .slideUp() when .stop() is called, the element will now still be displayed, but will be a fraction of its previous height. Callback functions are not called.

If more than one animation method is called on the same element, the later animations are placed in the effects queue for the element. These animations will not begin until the first one completes. When .stop() is called, the next animation in the queue begins immediately. If the clearQueue parameter is provided with a value of true, then the rest of the animations in the queue are removed and never run.

If the jumpToEnd argument is provided with a value of true, the current animation stops, but the element is immediately given its target values for each CSS property. In our above .slideUp() example, the element would be immediately hidden. The callback function is then immediately called, if provided.

As of jQuery 1.7, if the first argument is provided as a string, only the animations in the queue represented by that string will be stopped.

The usefulness of the .stop() method is evident when we need to animate an element on mouseenter and mouseleave:

<div id="hoverme">
  Hover me
  <img id="hoverme" src="book.png" alt="" width="100" height="123" />
</div>

We can create a nice fade effect without the common problem of multiple queued animations by adding .stop(true, true) to the chain:

$('#hoverme-stop-2').hover(function() {
  $(this).find('img').stop(true, true).fadeOut();
}, function() {
  $(this).find('img').stop(true, true).fadeIn();
});

Toggling Animations

As of jQuery 1.7, stopping a toggled animation prematurely with .stop() will trigger jQuery's internal effects tracking. In previous versions, calling the .stop() method before a toggled animation was completed would cause the animation to lose track of its state (if jumpToEnd was false). Any subsequent animations would start at a new "half-way" state, sometimes resulting in the element disappearing. To observe the new behavior, see the final example below.

Animations may be stopped globally by setting the property $.fx.off to true. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.

Examples:

Example: Click the Go button once to start the animation, then click the STOP button to stop it where it's currently positioned. Another option is to click several buttons to queue them up and see that stop just kills the currently playing one.

<!DOCTYPE html>
<html>
<head>
  <style>div { 
position: absolute; 
background-color: #abc;
left: 0px;
top:30px;
width: 60px; 
height: 60px;
margin: 5px; 
}
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="go">Go</button> 
<button id="stop">STOP!</button>
<button id="back">Back</button>
<div class="block"></div>
<script>
/* Start animation */
$("#go").click(function(){
$(".block").animate({left: '+=100px'}, 2000);
});

/* Stop animation when button is clicked */
$("#stop").click(function(){
$(".block").stop();
});

/* Start animation in the opposite direction */
$("#back").click(function(){
$(".block").animate({left: '-=100px'}, 2000);
});

</script>

</body>
</html>

Demo:

Example: Click the slideToggle button to start the animation, then click again before the animation is completed. The animation will toggle the other direction from the saved starting point.

<!DOCTYPE html>
<html>
<head>
  <style>.block { 
background-color: #abc;
border: 2px solid black;
width: 200px; 
height: 80px;
margin: 10px;
}
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="toggle">slideToggle</button> 
<div class="block"></div>
<script>
var $block = $('.block');
/* Toggle a sliding animation animation */
$('#toggle').on('click', function() {
    $block.stop().slideToggle(1000);
});
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .stop() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .stop()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • joelangeway

    Calling $(this).stop() from inside the completion callback of an animation will cause the final values of the animation to write over any previous changes in the callback and also call the callback again!

    For now I can just not call .stop() inside my callback, but it makes it harder to write methods that perform a particular animation which have to immediately complete any current animation of the same type on the same object.

  • http://daybreaker.info/ daybreaker

    I have experienced very strange behaviours with several animations like slide, which was that calling just stop() makes next-time animation to restore wrong state. For example, my div element is displayed at a completely wrong place from the second time after stopping at the middle of the first animation and this wrong position is even changed every time. (I could not find how it is calculated like that)

    To prevent such “strange” behaviours, use stop(true, true) to make jQuery properly finalize the animation with appropriate state. Yes, jumpToEnd is very important! It does more things than just setting CSS values to the target values.

    • Sinisa Rudan

      Agree!
      I used it in form stop(true), thus with jumpToEnd = false, and it blocked after few animations.

  • zocky

    It would be very nice if there were pause and continue methods for queues.

    • http://www.martinsmucker.com Michael Martin-Smucker

      Definitely. There has to be some good way to do this, right? After you've stopped the animation, is there any way to make it start up again from where it left off?

      • Blatte

        Juste use dequeue method.

        • http://www.martinsmucker.com Michael Martin-Smucker

          Thanks for the tip. Are there any good resources for learning more about dequeue() because I just read through the 1 paragraph of documentation several times and I still have no idea what it does.

  • Anonymous

    I just thought of this.What about adding a third option for the second argument, jumpToBack? It would now be called jumpTo and you could determine ‘where’. The third option would make the annimation peform in reverse, to it’s tarting position.

  • Gmartin Php

    I'm trying to do something like this:

    $('#hoverme-stop-2').hover(function() {
    $(this).find('#img1').stop(true, true).slideUp();
    $(this).find('#img2').stop(true, true).slideDown();
    }, function() {
    $(this).find('#img1').stop(true, true).slideDown();
    $(this).find('#img2').stop(true, true).slideUp();
    });

    So I need to create a rollover effect between 2 images using slide instead of fade… but it does funny strange stuff nothnig like using fade… :-

  • http://twitter.com/betalb Виталий Ищенко

    It should be mentioned, that onComplete handler is called if jumpToEnd is set to true

  • http://twitter.com/max_wtf Max

    Why stop() function called many-many times during simple slideDown/Up animation? I used firebug profiler – 40-50% of all functions time is stop() method. Some like this:
    Profiling (919.093ms, 12799 calls)
    stop() 1522 calls 40.92% 376.049ms 481.968ms 0.317ms 0.029ms 4.143ms jquery.js (line 145)

  • http://twitter.com/max_wtf Max

    Why stop() function called many-many times during simple slideDown/Up animation? I used firebug profiler – 40-50% of all functions time is stop() method. Some like this:
    Profiling (919.093ms, 12799 calls)
    stop() 1522 calls 40.92% 376.049ms 481.968ms 0.317ms 0.029ms 4.143ms jquery.js (line 145)

    Forgot – jquery version 1.4.2