我有一个div,一旦页面加载动画.然后我有一个按钮.单击此按钮后,我想制作一个新的div,并在动画之前为其设置动画,但是onclick()第一个div也会改变它的位置.我应该如何以某种方式禁用已经动画的div的动画效果.
<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);
});
});
解决方法:
按照以下方式,您只能移动新添加的div.
$(document).ready(function() {
var car = $('.car');
car.animate({left:"+=367px"}, 2000);
car.animate({top:"-=247px"}, 2000);
$("#button").click(function () {
$(".wrapper").append('<div class="car move">car</div>');
var car = $('.move');
car.animate({left:"+=367px"}, 2000);
car.animate({top:"-=247px"}, 2000);
$(".car").removeClass('move');
});
});
.car{
position:absolute;
left:0;
top:0;
}
.wrapper{
position:relative;
top:0;
left:0;
width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="car">car</div>
</div>
<button id="button">spawn new car</button>