jQuery stop() method is very useful for stopping the currently running animation before the finishing of the selected element animation.
General Syntax of animation:
$(selector).stop(stopAll,goToEnd);false
and it denotes whether or not to complete the current animation immediately.
Source Code
$(document).ready(function () {
$(".start-animation").click(function () {
$(".animated-box").animate({ width: 500 }, 2000);
});
$(".animated-box").click(function () {
$(".animated-box").stop();
});
});
Let us use both optional parameters of the stop()
method and set it to true.
Source Code
$(document).ready(function () {
$(".start-animation").click(function () {
$(".animated-box").animate({ width: 500 }, 2000);
$(".animated-box").animate({ width: 80 }, 3000);
});
$(".animated-box").click(function () {
$(".animated-box").stop(true, true);
});
});