1

I am trying to animate a small circle inside a box, using Jquery animate left function. It is working fine when I pass harcoded values. Like shown below, 474 is the resultant if I subtract circle width from box width.

    
        $(document).ready(function(){
             $("#start_a").toggle(function(){
                if ($("#start_a").html() === 'Roll'){
                    $("#start_a").html("Stop"); 
                    function start(){
                        $("#circle").animate({left: "+=474"},"slow",function(){
                            $("#circle").animate({left: "-=474"},"slow",function(){
                                start();
                            });
                        }); 
                    }
                    start();
                }
             },    
             function(){
                if ($("#start_a").html() === 'Stop'){
                    $("#start_a").html("Roll"); 
                    $("#circle").stop();
                    $("#circle").css("left", "initial");
                }
             });
        });       

To make it dynamic I am trying to do the same animation by passing variables as shown below. (Only pasting new Jquery).

$(document).ready(function(){
  
           var roll = $("#box").width() - $("#circle").width();
                 var effect = {};
                 var reverse = {};
                 effect['left'] = roll;
                 reverse['left'] = -roll;
                 $("#start_a").toggle(function(){
                    if ($("#start_a").html() === 'Roll'){
                        $("#start_a").html("Stop"); 
                        function start(){
                            $("#circle").animate(effect,"slow",function(){
                                $("#circle").animate(reverse,"slow",function(){
                                    start();
                                });
                            }); 
                        }
                        start();
                    }
                 },    
                 function(){
                    if ($("#start_a").html() === 'Stop'){
                        $("#start_a").html("Roll"); 
                        $("#circle").stop();
                        $("#circle").css("left", "initial");
                    }
                 });
            });

Circle is not animating as expected,it is going beyond the box border. Can someone tell me what might be the issue? Thanks in advance!

Below is my HTML/CSS

<div id="box">
            <div id="circle"></div>           
        </div>
        <div id="box2">
            <button id="start_a" >Roll</button>
<!--            <button id="stop_a">Stop</button>-->
        </div>

<style>    
        #circle {
	    width: 20px;
	    height: 20px;
	    background: black;
	    border-radius: 50px;
            left: 0px;
            position: absolute;
        } 
        
        #box{
            border: 3px solid green;
            background-color: white;
            margin-left: 400px;
            margin-top: 100px;
            width: 494px;
            min-height: 300px;
            position: relative;
        }
        #box2{
            /*border: 3px solid red;*/
            margin-left: 400px;
            /*margin-top: 100px;*/
            width: 494px;
            min-height: 50px;
            position: relative;
        }
        #start_a{
            margin-left: 200px;
            top: 0px;
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        #stop_a{
            /*margin-left: 720px;*/
            /*top: 0px;*/
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        </style>

2
  • Could you give us the corresponding HTML / CSS. A JSFIDDLE would be helpful as well. Commented Dec 4, 2015 at 7:17
  • @magreenberg - I have added my HTML/CSS. Please check now. Commented Dec 4, 2015 at 7:22

1 Answer 1

2

Ok please try:

$(document).ready(function(){
  
  var roll = $("#box").width() - $("#circle").width();
  var effect = {};
  var reverse = {};
  effect['left'] = roll;
  reverse['left'] = 0;
  $("#start_a").click(function(){
    if ($("#start_a").html() === 'Roll'){
      $("#start_a").html("Stop"); 
      start();
    }else{
      $("#start_a").html("Roll"); 
      $("#circle").stop();
      $("#circle").css("left", "initial"); 
    }
  })   

  function start(){
    $("#circle").animate(effect,"slow",function(){
      $("#circle").animate(reverse,"slow",function(){
        start();
      });
    }); 
  }
});
#circle {
	    width: 20px;
	    height: 20px;
	    background: black;
	    border-radius: 50px;
            left: 0px;
            position: absolute;
        } 
        
        #box{
            border: 3px solid green;
            background-color: white;
            margin-left: 100px;
            margin-top: 100px;
            width: 494px;
            min-height: 300px;
            position: relative;
        }
        #box2{
            /*border: 3px solid red;*/
            margin-left: 400px;
            /*margin-top: 100px;*/
            width: 494px;
            min-height: 50px;
            position: relative;
        }
        #start_a{
            margin-left: 0;
            top: 0px;
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
        #stop_a{
            /*margin-left: 720px;*/
            /*top: 0px;*/
            width: 100px;
            height: 50px;
            text-align: center;
            position: relative;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box">
  <div id="circle"></div>           
</div>
<div id="box2">
  <button id="start_a" >Roll</button>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

@Frank - I just tried your code. It is giving same result as mine. Can you please recheck.
ok i have update my answer. Your reverse array width must be the initial position. When you add -roll the position is -474 and no 0. Please try now
This is working. But please help me understand, why -474 while hard coding and why 0 while using widths.
Because with hard coding your element posA is 474px and you precises after posB -474px. so posA - posB (474 - 474 = 0) but with your code you precises posA is 474px and posB is (-posA), so your element go to 474px to -474px. Please is work for you solved it, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.