链接影响不同元素的jQuery动画

$(document).ready(function() {
    $("#div1").fadeIn("slow");
    $("#div2").delay(500).fadeIn("slow");
    $("#div3").delay(2000).fadeIn("slow");
    $("#div4").delay(8000).fadeIn("slow");
});

这是我目前的设置,但我觉得这不是写这个的最好方法。我找不到任何关于如何更好地写出时间的例子。任何帮助?我会很感激

我还应该补充说,每个元素的时间不一致。

解决方法

fadeIn将回调作为其第二个参数。一旦动画完成,就会执行回调。如果您希望元素依次淡入淡出,您可以链接回调:
$(document).ready(function() {
    $("#div1").fadeIn("slow",function(){
        $("#div2").fadeIn("slow",function(){
            $("#div3").fadeIn("slow",function(){
                $("#div4").fadeIn("slow");
            });
        });
    });
});

这可以使用一组选择器和一个单一的方法重写,如果你觉得这样:

var chain = function(toAnimate,ix){
    if(toAnimate[ix]){
        $(toAnimate[ix]).fadeIn('slow',function(){ chain(toAnimate,ix + 1 )});
    }
};

$(document).ready(function(){
    chain(['#div1','#div2','#div3','#div4'],0);
});

See this last one in action at JSBin

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...