【发布时间】:2012-07-27 22:12:41
【问题描述】:
我正在制作一些波浪图像以缓慢反复地上下移动,以赋予它更生动的感觉,我发现了 Javascript 的 setInterval()。这非常适合每 5.3 秒(以略有不同的速度)上下动画我的不同波浪图像。
self.setInterval("waves()",5300);
function waves() {
$('.waves1').animate({
bottom: '-5px'
}, 2000);
$('.waves1').animate({
bottom: '7px'
}, 3000);
$('.waves2').animate({
bottom: '-5px'
}, 2200);
$('.waves2').animate({
bottom: '7px'
}, 2800);
$('.waves3').animate({
bottom: '-5px'
}, 1800);
$('.waves3').animate({
bottom: '7px'
}, 3200);
}
在实现这个之后,我还发现你可以使用一个递归的 jQuery 函数来保持动画的继续。例如:
function animateWaves() {
$('.waves1').animate({ top: '+=15' },
{ duration: 2000, easing: "linear" })
.animate({ top: '-=15' },
{ duration: 3000, easing: "linear",
complete: animateWaves });
}
$(function() {
animateWaves();
});
其中一个(递归或 setInterval)相对于另一个有什么优势?
【问题讨论】:
标签: javascript jquery recursion jquery-animate setinterval