In the previous jQuery tutorial, Mytour introduced hiding and showing HTML elements with jQuery. In this tutorial, Mytour will further delve into the stop() method in jQuery for ceasing dynamic effects.
jQuery Stop() Method Demystified
1. The stop() Method in jQuery
The stop() method in jQuery is used to halt running jQuery effects on the selected elements before the effects are completed.
The basic syntax of the stop() method in jQuery looks like the following:
$(selector).stop(stopAll, goToEnd);
1. Parameters:
- The stopAll parameter (optional) specifies whether to clear animations in the queue. The default value is False, meaning the current animation will stop, and the remaining animations in the queue will run afterward.
- The goToEnd parameter (optional) determines whether to stop the animation immediately. The default value is False.
Example: The example below illustrates the stop() method in jQuery. You can click the button to start and pause the animation:
1. Result Format:
Important: The stop() method works with all jQuery effects, including fade, slide, show/hide animations, as well as custom animations.
Example: Here is another example illustrating the stop() method in jQuery:
2. Result Output: The returned result looks like the following:
2. Create Mouse Hover Effects
Mouse hover effects are a type of animation, but when we hover, the effect will display and disappear quickly. This is because the mouseenter or mouseleave events are triggered rapidly before the animation completes.
To limit and avoid this issue, we add the stop(true, true) method to the method chain to create smoother mouse hover effects, as shown in the example below:
Result Output: The returned result looks like the following:
Note: The stop(true, true) method in jQuery will clear all animations in the queue and move the current animation value to the final one.
So, in this tutorial, Mytour has just introduced you to the stop() method in jQuery to temporarily pause dynamic effects. If you have any concerns or questions, readers can leave their comments below the article.
In the next tutorial, Mytour will introduce you to Getter and Setter methods in jQuery.