延迟JQuery效果

我想延迟几秒钟后淡出一个元素及其所有子元素。 但我还没有find一种方法来指定效果应该在指定的时间延迟后开始。

setTimeout(function() { $('#foo').fadeOut(); }, 5000); 

5000是五毫秒。

我使用这个暂停插件,我刚刚写道

 $.fn.pause = function(duration) { $(this).animate({ dummy: 1 }, duration); return this; }; 

像这样调用它:

 $("#mainImage").pause(5000).fadeOut(); 

注意:你不需要callback。


编辑:你现在应该使用jQuery 1.4。 内置的delay()方法。 我没有检查,但我认为它比我的插件更“聪明”。

以前你会做这样的事情

 $('#foo').animate({opacity: 1},1000).fadeOut('slow'); 

第一个animation没有做任何事情,因为你已经在元素上有不透明度1,但是会暂停一段时间。

在jQuery 1.4中,他们已经将其构build到框架中,所以您不必像上面那样使用hack。

 $('#foo').delay(1000).fadeOut('slow'); 

该function与原始jQuery.delay()插件相同http://www.evanbot.com/article/jquery-delay-plugin/4

最好的方法是使用jQuery延迟方法:

$( '#添加my_id')延迟(2000).fadeOut(2000年)。

你可以通过使用fadeTo()方法避免使用setTimeout,并设置一个5秒的延迟。

 $("#hideAfterFiveSeconds").click(function(){ $(this).fadeTo(5000,1,function(){ $(this).fadeOut("slow"); }); }); 

我已经写了一个插件,让你添加到链延迟。

例如$('#div')。fadeOut()。delay(5000).fadeIn(); //淡出元素,等待5秒钟,淡入淡出元素。

它不使用任何animation黑客或过度的callback链接,只是简单的干净的短代码。

http://blindsignals.com/index.php/2009/07/jquery-delay/