关于您编辑的问题。您想在哪里插入内容或完全更改内容,我做了一个新的小提琴。
在这个演示中,您有一个按钮,当您单击它时,它会向包装器 div 添加另外两个 Lorem Ipsum 段落。它的方式是首先将 div 动画到适当的高度,然后添加内容。您可以多次单击该按钮,每次它都会添加一个新的演示内容块。
HTML 标记:(您应该注意的是,我使用了一个临时容器来存放可见性设置为隐藏的内容。)
<a href="#" id="change_content">Add content</a>
<div id="wrapper">
<div class="content_box">
<p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ei
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
</div>
</div>
<div id="temp_content"></div><!-- Container for temporary content -->
CSS(只是在这里使用 bg 颜色使动画可见,并且 visibility: hidden 用于临时容器。请注意,您不能使用 display: none 因为在测量高度时会得到 0)
#wrapper {background-color: orange;}
.content_box {width: 400px; background-color: green;}
#temp_content {width: 400px; visibility: hidden;}
jQuery(我在这里注释了代码)
$(document).ready(function() {
$('#change_content').click(function(e) {
/*Some demo content here. Two new paragraphs wrapped in the div with content_box
class. If it wasn't wrapped in the content_box class. In order to get correct
height you should have temporary container width same as desired target
container width so the content fills up properly.*/
var new_content = '<div class="content_box"><p>Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</p></div>';
//insert the new content inside the temporary container.
$('#temp_content').html(new_content);
//calculate the temporary container height
var new_content_height = $('#temp_content').height();
//calculate current content height
var existing_content_height = $('#wrapper').height();
//add two heights together
var target_height = new_content_height + existing_content_height + 'px';
$('#wrapper').animate({
height: target_height
}, 1000, function() {
//after the animation is over insert the content to the wrapper
$('#wrapper').append(new_content);
//its not obligatory but lets empty the temporary container
$('#temp_content').empty();
});
});
});
希望能帮助到你。
编辑:有错误的小提琴链接。现在它指向正确。