我试图让一个元素淡入,然后在5000毫秒后再次淡出.我知道我可以做一些事情,比如:

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

但这只会控制淡出,我会在回调中添加上面的内容吗?

推荐答案

Update:从jQuery1.4开始,您可以使用.delay( n )方法.http://api.jquery.com/delay/

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

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});

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'); 

}); 

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);

从理论上讲,这将允许你这样做:

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

Jquery相关问答推荐

为什么如果使用转换规模,juserui可拖动遏制不起作用

提交表单时运行自定义验证脚本

Bootstrap 的工具提示在悬停时将表格单元格向右移动一点

寻找 jQuery 事件类型的完整列表

jQuery 的元素或类 LIKE Select 器?

如何在 jquery 中从这个日期减go 一周?

JQuery/Javascript:判断 var 是否存在

Google Maps API v3 infowindow 关闭事件/回调?

ASP.Net 母版页和文件路径问题

在没有 jQuery 的情况下在 node.js 上合并或合并 JSON

ajax调用后jQuery点击功能不起作用?

使html文本输入字段随着我输入而增长?

使用jquery设置di​​v标签的值

在完成前一个请求之前中止新的 AJAX 请求

使用 JQuery 获取触发事件的元素的类

如何从 jQuery UI datepicker 获取日期

如何让 JavaScript/jQuery Intellisense 在 Visual Studio 2008 中工作?

带有 LIKE 的 Spring JPA @Query

添加到数组 jQuery

使用 TypeScript 设置 window.location