[英]Animate div height to fit new content
I want to animate div height to fit new content with slide down animation. 我想设置div高度的动画,以使新内容与下滑动画配合。
html: HTML:
<div id="container">
row 1<br>
row 2<br>
row 3<br>
</div>
javascript: JavaScript的:
var container = $('#container');
container.click(function() {
container.hide();
$(this).html(
'row 1<br>row 2<br>row 3<br>row 4<br>row 5<br>row 6<br>'
).slideDown(500);
});
The problem is that animation start from top, not from current height. 问题是动画从顶部开始,而不是从当前高度开始。
jsfiddle: https://jsfiddle.net/Lgt4a7fr/3/ jsfiddle: https ://jsfiddle.net/Lgt4a7fr/3/
You can use for the jQuery animate()
. 您可以将其用于jQuery
animate()
。 Please see the below demo. 请参见下面的演示。
and also i was looking on your below part of the code. 而且我也在看你下面的代码部分。 please think of appending the new items alone ?
请考虑单独添加新项目吗?
element.html(
'row 1<br>row 2<br>row 3<br>row 4<br>row 5<br>row 6<br>'
);
Kindly ignore if you have used this code just for demo purpose. 如果您只是出于演示目的使用此代码,请忽略。
var container = $('#container'); container.click(function(e) { var element = $(this); var height = element.height(); element.hide(); element.html( 'row 1<br>row 2<br>row 3<br>row 4<br>row 5<br>row 6<br>' ); // please think of appending the new items alone? var height2 = element.height(); element.css({height : height}); element.show(); element.animate( {height : height2} , 500); });
#container { border: 1px solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> row 1<br> row 2<br> row 3<br> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.