2011-08-08 223 views
5

如何使用jQuery延迟动画?jQuery动画延迟

我需要一个导航来扩大宽度,然后展开高度,然后反转为反向动画。

代码:

$(function() { 
    $("#nav li").not("#logo, #nav li ul li").hover(function(){ 
     $(this).animate({width:"200px"},{queue:false,duration:1000}); 
    }, function(){ 
     $(this).animate({width:"30px"},{queue:false,duration:1000}); 
    }); 


    $("#nav li.parent").hover(function(){ 
     $(this).children("ul").animate({height:"40px"},{queue:false,duration:500}); 
    }, function(){ 
     $(this).children("ul").animate({height:"0px"},{queue:false,duration:500}); 
    }); 
}); 

回答

9

使用的jQuery .delay(N)方法,其中N是延迟毫秒。

jsFiddle example

+1

我假设你的jsfiddle将清除此,但其中究竟那一点代码去了?我对jQuery非常陌生,所以对noob问题感到抱歉 – JacobTheDev

+0

动画之间,我添加了jsFiddle,随时去看看。 –

+0

啊,我明白了,但是如果我想让它处于两种不同的悬停状态,就像我的代码示例一样? – JacobTheDev

4

这里是叫你正在寻找http://api.jquery.com/delay/

.delay(n) // where n is the millis of delay 

用途如下

$.animate(action1).delay(n).animate(action2) 
// this code puts the delay bewtween two different animations