One of the most annoying issues in web design is when a user repeatedly hovers or clicks
an element and animations start "piling up." The stop() method is jQuery's
solution to this problem — it allows you to instantly halt a running animation before
starting a new one.
By default, jQuery creates a queue for animations. If you trigger an animation while another is already running, jQuery waits for the first one to finish. If a user hovers 5 times quickly, the animation will play back 5 times in a row, even after the user has stopped moving their mouse.
stop() on drop-down menus or
sliding panels prevents the menu from "bouncing" or opening and closing repeatedly when
moused over fast.
The stop() method can be used with or without parameters:
$(selector).stop(stopAll, goToEnd);
| Parameter | Default | Description |
|---|---|---|
| stopAll | false | If true, it clears the entire animation queue. The current animation stops, and no future ones in the queue will run. |
| goToEnd | false | If true, it forces the current animation to finish instantly (skipping the transition). |
Calling stop() without parameters stops only the currently active
animation, allowing the next one in the queue to begin.
$("#panel").stop().slideToggle();
Stops everything immediately, even if there are multiple animations lined up.
$("#box").stop(true).animate({width: '200px'});
Stops the animation and snaps the element immediately to its final CSS value.
$("#box").stop(true, true).fadeOut();
Compare how much smoother a hover menu feels when you use stop():
// Without stop() - Causes jittering
$("#menu").hover(function() {
$(this).find(".submenu").slideDown();
}, function() {
$(this).find(".submenu").slideUp();
});
// WITH stop() - Smooth and responsive
$("#menu").hover(function() {
$(this).find(".submenu").stop().slideDown();
}, function() {
$(this).find(".submenu").stop().slideUp();
});
slideToggle(),
fadeToggle(), or custom animate() methods triggered by hover
or fast-clicking, always prefix them with .stop().