首页 > jQuery animate height auto问题

jQuery animate height auto问题

$('.tree li .tree-primary-items').click(function(){
    $(this).animate({height: "auto"});
});

我设置高度为0px,overflow:hidden,当点击时height为auto,为什么不行?height为100px又可以?


可以先动态获取height,才传值给height属性。


要获取实际的高度

var $obj = $('.tree li .tree-primary-items');   
var currentHeight = $obj.css("height");
$obj.css("height","auto");
var animateHeight = $obj.css("height");
$obj.css("height", currentHeight);
$obj.animate({
     height: animateHeight
     }, 500);

建议多看看官方文档

Animation Properties and Values(动画属性和值)

所有用于动画的属性必须是数字的,除非另有说明;这些属性如果不是数字的将不能使用基本的jQuery功能。(例如,width, height或者left可以执行动画,但是background-color不能,除非使用jQuery.Color插件。)属性值的单位像素(px),除非另有说明。单位em 和 %需要指定使用。

【热门文章】
【热门文章】