如何循环jQuery div动画?
我正在尝试实现一个带有无限循环的jQuery函数来设置div的动画。我不知道该怎么做。这是我的代码:如何循环jQuery div动画?,jquery,loops,jquery-animate,Jquery,Loops,Jquery Animate,我正在尝试实现一个带有无限循环的jQuery函数来设置div的动画。我不知道该怎么做。这是我的代码: $(document).ready(function () { $('#divers').animate({ 'margin-top': '90px' }, 6000).animate({ 'margin-top': '40px' }, 6000); }); animate()函数可以选择在动画结束时调用函数。您可以执行相同的调用,然后瞧
$(document).ready(function () {
$('#divers').animate({
'margin-top': '90px'
}, 6000).animate({
'margin-top': '40px'
}, 6000);
});
animate()
函数可以选择在动画结束时调用函数。您可以执行相同的调用,然后瞧。将执行完整动画的代码放入一个函数中,然后将该函数作为回调参数传递给最后一个动画。类似于
$(document).ready(function() {
function ani() {
$('#divers').animate({
'margin-top':'90px'
},6000).animate({
'margin-top':'40px'
},6000, ani); //call the function again in the callback
});
});
ani();
});
$(document).ready(function() {
function animateDivers() {
$('#divers').animate(
{'margin-top':'90px'}
,6000
)
.animate(
{'margin-top':'40px'}
,6000
,animateDivers //callback the function, to restart animation cycle
);
}
animateDivers(); //call, to start the animation
});
您还可以设置set interval函数,指定在什么时间间隔调用哪个方法
$(function () { setInterval(fnName, 6000); });
使用.animate()
回调“调用”函数:
使用setInterval是一个不错的选择。太多的递归只会导致“堆栈溢出”:-)
“Uncaught RangeError:超出了最大调用堆栈大小”现在我明白了,我只需要查看代码来了解如何执行。非常感谢,它工作得很好!!谢谢你花时间放一个很棒的演示。一定要做好!谢谢大家的时间和耐心。嘿,谢谢大家。但你们能告诉我,若我想移动使用画布放置的图像,该怎么办?
$(function() {
function loop(){
$('#divers')
.animate({marginTop:90},6000)
.animate({marginTop:40},6000, loop); // callback
}
loop(); // call this wherever you want
});