Start and Stop an Animation in jQuery

Starting and Stopping a jQuery Animation is as simple as using the animate() and stop() functions, as shown in the example below:

<html xmlns="" >
<title>Start and Stop an Animation using jQuery</title>
<script type="text/javascript"
   2:     <style type="text/css">
   3:         div { 
   4:             background-color:#ffeecc; 
   5:             width:100px; 
   6:             height:100px;
   7:         }
   8:     </style>
   9:     <script type="text/javascript">
  10:         $(function () {
  11:             $('#btnStart').click(function () {
  12:                 $('#divOne').animate({
  13:                     width: "200px",
  14:                     height: "300px"
  15:                 }, 5000);
  16:             });
  18:             $('#btnStop').click(function () {
  19:                 $('#divOne').stop();
  20:             });
  21:         });
<input id="btnStart" type="button" value="Start" />
<input id="btnStop" type="button" value="Stop" />
<br /><br />
<div id="divOne" />


As you can see, when the user hits the Start Button, we first start animating the Height and Width of ‘divOne’, using the animate() function. On clicking the Stop button, we use the stop() function to stop the animation.

See a Live Demo

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: