2012-02-09 74 views
5

我有一個高度爲125px的ul。當用戶懸停UL時,我希望高度會動畫到高度自動。而當用戶超出UL認可的125ppx再次。jQuery動畫高度爲自動

$('.box .box-overflow ul').hover(function() { 
     $(this).animate({ 
      height: '100%' 
     }, 400); 
    }, function() { 
     $(this).animate({ 
      height: '125px' 
     }, 400); 
    }); 

這個工作,但當用戶進來ul它擴大但不具有良好的動畫效果?

有人能幫助我嗎? :)

+0

結帳的'easing'參數[動畫()](http://api.jquery.com/animate/)功能 – ManseUK 2012-02-09 10:36:54

+0

什麼是「不漂亮的動畫影響」? – ggzone 2012-02-09 10:36:54

+0

@ggzone就像你用CSS做懸停一樣......沒有動畫。只是在那裏他擴大。 – Maanstraat 2012-02-09 10:39:07

回答

8

你可以用scrollHeight屬性做到這一點。

$('ul').hover(function(){ 
    $(this).animate({ 
    height: $(this)[0].scrollHeight+'px' 
    }, 400); 
}, function(){ 
    $(this).animate({ 
    height: '125px' 
    }, 400); 
}); 
3

嘗試這樣懸停:

var height = $(this).css('height','auto').height(); // get real height 
$(this).css('height','125px'); // return current state; 
$(this).animate({height: height+'px'}, 400); 

的activivty在頭兩行不應該由用戶蜜蜂看到的,​​但你可以得到你的UL的實際高度。只有當你知道最終身高時,才能做出幻想的滑動效果。

工作例子是在這裏:http://jsfiddle.net/axpFk/

+0

這也不起作用。請參閱:http://jsfiddle.net/tDJzD/ – Maanstraat 2012-02-09 10:46:32

+2

你沒有它的權利,我意味着這個:http://jsfiddle.net/axpFk/ – Antonio 2012-02-09 12:38:05

+0

對不起:)我投你的帖子到 – Maanstraat 2012-02-10 13:58:40