【问题标题】:How to use delay() with each()?如何将延迟()与每个()一起使用?
【发布时间】:2011-07-13 06:36:40
【问题描述】:
$("#home #oneTool").prepend($(".userInfo.module"));

var topVal = $(".userInfo.module").height();
$(".userInfo.module").hide();
$(".userInfo.module").slideDown(3000);

$("#home #oneTool div.divspot").each(function(){
   var newVal = topVal + parseInt($(this).css('top'));
   $(this).css('top',newVal);
});

.userInfo.module首先出现在div.divspots...

由于我使用的是slideDown,所以每个函数都需要延迟,以便div.divspots也可以顺利滑下..(延迟会有帮助吗?)

注意:所有div.divspots 都是绝对定位的

【问题讨论】:

  • 如果不能直接使用延迟,这里有一个可能的竞争者。 setTimeout 或延迟加队列:stackoverflow.com/questions/6641222/…
  • 如果您将滑块移到 each 之后会发生什么?它是隐藏的,所以它甚至显示幻灯片?

标签: javascript jquery css delay slidedown


【解决方案1】:

首先,slideDown 将在完成后调用的函数作为参数。 http://api.jquery.com/slideDown/

向另一位意识到您在问什么的回答者提供支持,并为我没有正确阅读您的问题而给我一个#fail。

话虽如此,对于完成后没有回调的方法,这里有几个选项。

两种选择:

  1. 使用队列
  2. 使用 setTimeout

使用队列:

http://api.jquery.com/queue/

$(".userInfo.module").fooThatTakes(3000).queue(function() {
  $("#home #oneTool div.divspot").each(function(){
    var newVal = topVal + parseInt($(this).css('top'));
    $(this).css('top',newVal);
  });
});

使用 setTimeout。

var delay = 3000;
$(".userInfo.module").fooThatTakes(delay);
t = setTimeout(afterFoo, delay);

function afterFoo() {
  $("#home #oneTool div.divspot").each(function(){
    var newVal = topVal + parseInt($(this).css('top'));
    $(this).css('top',newVal);
  });
}

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,那么您想在 slideDown 完成后运行您的 each 函数。如果这是正确的,那么您可以使用对 slideDown 函数的回调:

    $(".userInfo.module").slideDown(3000, function() {
        //Your each function
    });
    

    回调将在动画完成后运行。

    【讨论】:

    • 哦不..我想向下滑动并并行运行每个函数
    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2015-04-21
    相关资源
    最近更新 更多