I have a div that animates as soon as the page loads. Then I have a button. After one click on this button, I want to create a new div and animate it, since it is animated by the previous one, but onclick () the first div also changes its position. How can I do this to somehow disable the animation effect on a div that is already animated.
<div class="wrapper">
<div class="car"></div>
</div>
<button id="button">spawn new car</button>
$(document).ready(function() {
var car = $('.car');
car.animate({left:"+=367px"}, 2000);
car.animate({top:"-=247px"}, 2000);
$("#button").click(function () {
$(".wrapper").append('<div class="car"></div>');
var car = $('.car');
car.animate({left:"+=367px"}, 2000);
car.animate({top:"-=247px"}, 2000);
});
});
source
share