Your JavaScript code is a little dirty. First of all, your move function:
function move(){ $("input").click(function(){ $(this).val('Stop'); $("div").css({left:'0%'}).animate({left:'100%'},1000, Stop); Stop(); }); }
The Stop function (and not the function defined as the callback function of the animation function) will be called while the div is being animated. You do not want this.
What you may want are essentially three different functions:
Your move function will essentially begin to move your object, a pause will obviously pause it, and reset will put your object in its original position if you want to.
Let's say your HTML file is structured as follows:
<h1 style="text-align:center"> Welcome to the test</h1> <input type='button' value='Start' id='oneButton' /> <div id="object"> <img id='myRobot' alt='test' src='http://www.clker.com/cliparts/7/b/d/b/1237099752389782475nicubunu_Soccer_ball.svg.thumb.png'/> </div>
Your CSS:
#object { position: absolute; left: 0px; bottom: 100px; width: 100px; height: 100px; }
And finally, JS:
var animating = false; $("#oneButton").on("click", function() { if (!animating) { $(this).val("pause"); move(); } else { $(this).val("start"); pause(); } }); function move() { animating = true; $("#object").animate({left:'100%'},1000, reset); } function pause() { $("#object").stop(); animating = false; } function reset() { animating = false; $("#object").css({left: '0%'}); }
Here is the FIDDLE where you can see the "action".
source share