The jQuery stop()
method is used to stop an animation or effect before it is finished.
The stop()
method works for all jQuery effect functions, including sliding, fading and custom animations.
Syntax:
$(selector).stop(stopAll,goToEnd);
The optional stopAll parameter specifies whether also the animation queue should be cleared or not. Default is false, which means that only the active animation will be stopped, allowing any queued animations to be performed afterwards.
The optional goToEnd parameter specifies whether or not to complete the current animation immediately. Default is false.
So, by default, the stop()
method kills the current animation being performed on the selected element.
The following example demonstrates the stop()
method, with no parameters:
Step 1:Create index.html file and implement below code.
<button class="btn-success" id="stop">Stop sliding</button> <div id="flip">Click to slide down panel</div> <div id="panel">Hello world!</div>
Step 2:Implement jquery to use stop method to stop the animation.
<script> $(document).ready(function(){ $("#flip").click(function(){ $("#panel").slideDown(5000); }); $("#stop").click(function(){ $("#panel").stop(); }); }); </script>
Complete Code For Stopping Animation In JQuery
<!DOCTYPE html> <html> <head> <title>How Can I Use Stop Method For Animations In JQuery</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <style> body { background: darkolivegreen; } .well{ height:210px; } #panel, #flip { padding: 5px; font-size: 18px; text-align: center; background-color: #555; color: white; border: solid 1px #666; border-radius: 3px; } #panel { padding: 50px; display: none; } </style> <body> <div class="container"> <br><br><br> <div class="text-center"> <h2 id="color" style="color: White">Use Stop Method For Animations In JQuery</h2> </div> <br> <br> <div class="well"> <button class="btn-success" id="stop">Stop sliding</button> <div id="flip">Click to slide down panel</div> <div id="panel">Hello world!</div> </div> </div> </body> </html> <script> $(document).ready(function(){ $("#flip").click(function(){ $("#panel").slideDown(5000); }); $("#stop").click(function(){ $("#panel").stop(); }); }); </script>