如何为animate()
动态获取元素的高度?
这是我的代码,用于显示元素中的更多隐藏文本。问题是我必须将动画高度(例如300px)修复为固定数字,并且文本会不断更改,所以我不知道它会持续多长时间。
$('.toggle-slide').click(function() {
var object = $(this);
var object_target = $('.text-about');
var target_height = object_target.height();
object_target.animate({
height:'300px'
}, 1500 );
return false;
});
所以我认为应该动态获取元素的实际高度,然后我可以将此参数传递给animate()
。
但我怎样才能获得这个动态高度?
或许你还有其他更好的想法?
这是我的测试link。
答案 0 :(得分:1)
$('.toggle-slide').click(function() {
var object = $(this);
var object_target = $('.text-about');
var target_height = object_target.height('100%').height();
object_target.height('200px');
object_target.animate({
height: target_height + 'px'
}, 1500 );
return false;
});
我首先让它全部可见,然后取高度,然后回到200px并制作动画:)
可能在较慢的机器上闪烁,但它的想法