2012-02-09 72 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