How to control animate function on button click?


I have two divs named “arrow” and “inner”. I am trying to control the animate slide function when the div is clicked but have been unfortunate. The issue is noticeable when clicking very fast on the “arrow” div after user stops clicking the div is still sliding. I set the animate function under a small delay but I still experience lag. Here is my example code:

     <script language="javascript" src=""></script>
    <script language="javascript">
    $(document).ready(function() {
          var out = 0;
              $("#inner").animate({marginRight: "0px"}, 500 );
             $("#inner").delay(400).animate({marginRight: "-100px"}, 500 );

    <div style="background-color: rgb(204, 204, 204); height: 300px; width: 300px; overflow: hidden; position: relative;">
<div id="inner" style="height: 100px; width: 150px; background-color: rgb(0, 204, 102); float: right; margin-right:-150px;" >Form is here</div>

<div id="arrow" style="height: 100px; width: 50px; background-color: rgb(255, 0, 0); float: right; cursor: pointer; position: absolute; top: 0; right: 0;" >Arrow is here</div>



Just change your code

$("#inner").animate({marginRight: "0px"}, 500 );


$("#inner").stop(true, true).animate({marginRight: "0px"}, 500 );


$("#inner").animate({marginRight: "-100px"}, 500 );


$("#inner").stop(true, true).animate({marginRight: "-100px"}, 500 );

Please see following Link for example :

you can also use $(“#inner”).stop(true, false).animate() instead of $(“#inner”).stop(true, true).animate(). as per your choice.

Answered By – Rony SP

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.