【问题标题】:Jquery: how to sleep or delay?Jquery:如何睡觉或延迟?
【发布时间】:2010-05-30 19:30:05
【问题描述】:

我想向上移动对象,延迟 1000 毫秒,然后隐藏它,

我得到了代码:

$("#test").animate({"top":"-=80px"},1500)
      .animate({"top":"-=0px"},1000)
      .animate({"opacity":"0"},500);

我用".animate({"top":"-=0px"},1000)"来实现延迟,效果不好。

我想要:

$("#test").animate({"top":"-=80px"},1500)
      .sleep(1000)
      .animate({"opacity":"0"},500);

有什么想法吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    .delay() 怎么样?

    http://api.jquery.com/delay/

    $("#test").animate({"top":"-=80px"},1500)
              .delay(1000)
              .animate({"opacity":"0"},500);
    

    【讨论】:

    • 这在这种情况下有效,但告诉线程睡眠和延迟效果是两件完全不同的事情。 jQuery 甚至在他们的文档中提醒了这一点。
    【解决方案2】:

    如果您不能按照 Robert Harvey 的建议使用 delay 方法,则可以使用 setTimeout

    例如。

    setTimeout(function() {$("#test").animate({"top":"-=80px"})} , 1500); // delays 1.5 sec
    setTimeout(function() {$("#test").animate({"opacity":"0"})} , 1500 + 1000); // delays 1 sec after the previous one
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多