我有一個固定的高度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