jQuery is a JavaScript library widely used in web development. Its flexible animation function can effectively improve the user experience of the website. In actual development, many developers have encountered situations where they need to stop the animation being executed. This article will introduce how to stop jQuery's animation effect.
1. Use the stop() method
jQuery provides a stop() method, which can be used to stop the ongoing animation effect. The syntax of this method is as follows:
$(selector).stop(stopAll, gotoEnd);
Among them, selector is the element to be animated; stopAll is an optional parameter, used to specify whether to stop all animation effects in progress (the default value is false, that is, only Stop the current animation); gotoEnd is another optional parameter used to specify where to stop the animation effect (the default value is false, which means stopping at the current position).
The following is a simple example that demonstrates how to stop a running animation:
$(document).ready(function(){ $("button").click(function(){ $("#box").animate({left: '250px'}, 5000); }); $("#stop").click(function(){ $("#box").stop(); }); });
In this example, when the user clicks the button, the element with the id "box" will be An animation effect that lasts 5 seconds and moves to the left. When the user clicks on the element with the id "stop", the animation will be stopped.
2. Use the clearQueue() method
In addition to the stop() method, jQuery also provides a clearQueue() method for clearing the queue on the element. Normally, when an animation effect is stopped, all animations on the queue are cleared. However, if you don't want to stop the current animation, but want to clear other animation effects, then the clearQueue() method will be very useful.
The syntax of this method is as follows:
$(selector).clearQueue(queueName);
Among them, queueName is an optional parameter used to specify the name of the queue to be cleared. If this parameter is not specified, all animation effects on the default queue will be cleared.
The following is an example that demonstrates how to clear other animation effects except the current animation when stopping the animation:
$(document).ready(function(){ $("button").click(function(){ $("#box").animate({left: '250px'}, 5000); $("#box").animate({top: '100px'}, 5000); }); $("#stop").click(function(){ $("#box").stop(false, true).clearQueue(); }); });
In this example, when the user clicks the button, the id will be Perform an animation effect for the "box" element to first move to the left and then move down. When the user clicks on the element with the id "stop", the downward movement animation will be stopped, but the left movement animation effect will be retained. At the same time, other animation effects are also cleared.
Summary:
If you want to stop the running animation effect, you can use the stop() method; if you want to clear other animation effects in the queue when stopping the animation, you can use clearQueue() method. In actual development, choose different methods as needed to achieve your goals.
The above is the detailed content of jquery stop animate. For more information, please follow other related articles on the PHP Chinese website!