我正在尝试使用无限循环的jQuery函数来动画化一个div,但是我不知道该怎么做。以下是我的代码:
$(document).ready(function () {
$('#divers').animate({
'margin-top': '90px'
}, 6000).animate({
'margin-top': '40px'
}, 6000);
});
我正在尝试使用无限循环的jQuery函数来动画化一个div,但是我不知道该怎么做。以下是我的代码:
$(document).ready(function () {
$('#divers').animate({
'margin-top': '90px'
}, 6000).animate({
'margin-top': '40px'
}, 6000);
});
$(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
});
$(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();
});
使用.animate()
回调函数来“调用”你的函数:
$(function() {
function loop(){
$('#divers')
.animate({marginTop:90},6000)
.animate({marginTop:40},6000, loop); // callback
}
loop(); // call this wherever you want
});
使用setInterval是正确的方法。过多的递归会导致"堆栈溢出"错误:)
"未捕获的RangeError:超出最大调用堆栈大小"
您还可以设置setInterval函数,指定在什么间隔调用哪个方法。
$(function () { setInterval(fnName, 6000); });
animate()
函数有一个选项,可以在动画结束时调用一个函数。您可以直接进行相同的调用,完成。