jQuery超时效果 [英] Timeout jQuery effects

查看:57
本文介绍了jQuery超时效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让某个元素淡入,然后在5000毫秒内再次淡出.我知道我可以做类似的事情:

I am trying to have an element fade in, then in 5000 ms fade back out again. I know I can do something like:

setTimeout(function () { $(".notice").fadeOut(); }, 5000);

但是那只能控制淡出,我可以在回调函数中添加上面的内容吗?

But that will only control the fade out, would I add the above on the callback?

推荐答案

更新:从jQuery 1.4开始,您可以使用.delay( n )方法. http://api.jquery.com/delay/

Update: As of jQuery 1.4 you can use the .delay( n ) method. http://api.jquery.com/delay/

$('.notice').fadeIn().delay(2000).fadeOut('slow'); 

注意:默认情况下,$.show()$.hide()不排队,因此,如果要与它们一起使用$.delay(),则需要这样配置:

Note: $.show() and $.hide() by default are not queued, so if you want to use $.delay() with them, you need to configure them that way:

$('.notice')
    .show({duration: 0, queue: true})
    .delay(2000)
    .hide({duration: 0, queue: true});


您可能会使用Queue语法,这可能会起作用:


You could possibly use the Queue syntax, this might work:

jQuery(function($){ 

var e = $('.notice'); 
e.fadeIn(); 
e.queue(function(){ 
  setTimeout(function(){ 
    e.dequeue(); 
  }, 2000 ); 
}); 
e.fadeOut('fast'); 

}); 

或者您可能真的很聪明,可以使用jQuery函数来做到这一点.

or you could be really ingenious and make a jQuery function to do it.

(function($){ 

  jQuery.fn.idle = function(time)
  { 
      var o = $(this); 
      o.queue(function()
      { 
         setTimeout(function()
         { 
            o.dequeue(); 
         }, time);
      });
  };
})(jQuery);

(理论上,可以在这里处理内存)可以允许您这样做:

which would ( in theory , working on memory here ) permit you do to this:

$('.notice').fadeIn().idle(2000).fadeOut('slow'); 

这篇关于jQuery超时效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆