这个问题类似于
this one about animation queues,但该主题中的答案非常具体,不容易推广.
在我的Web应用程序中,通过输出class =“alert”的div,将消息报告给用户(信息框,错误和警告等),例如:
<div class="alert">Login successful</div> <div class="alert">You have new messages waiting</div>
页面上可能有任意数量的警报,具体取决于用户的操作.
我想要的是使用jQuery动画按顺序显示每个警告框:一旦一个动画结束,下一个动画开始.
其他问题的答案说使用回调如:
$('#Div1').slideDown('fast',function(){
$('#Div2').slideDown('fast');
});
除非在动画中存在未知数量的div,否则无效.有人知道实现这个目标的方法吗?
解决方法
我想出了一个解决方案,虽然我怀疑有人知道更多关于JS闭包的人可能会给我一些指示.
nextAnim($('.alert'));
function nextAnim($alerts) {
$alerts
.eq(0) // get the first element
.show("slow",function() {
nextAnim($alerts.slice(1)); // slice off the first element
}
)
;
}