我有一个固定的高度div与动态内容。当某些内容添加到div时,我希望自动滚动到底部。我发现scrollTop = scrollHeight
解决方案,但它不符合我的条件。滚动到具有动态内容的div的底部
$(document).ready(function(){
var text = '<p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p><p>some text</p>'
$("#content").html(text);
$("#content").scrollTop($('#content').scrollHeight)
})
#content{
max-height:250px;
overflow-y:auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content"></div>
'.height()'会输出容器的高度(在本例中为250px,因为'max-height:250px'。这无助于获得“可滚动高度” –
@MichaelWalter感谢指出出问题 – Ashish451