我有一个div,里面有一个聊天盒。每当有新消息到达时,它就会被附加到框中--我希望自动将框滚动到底部(以显示新消息)。我如何才能做到这一点呢?
<div id="chatting" style="overflow: auto; background-color: #fff; border: solid 2px #dedede; width: 600px; height: 500px; padding: 10px;">
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
hello<br/>
</div>发布于 2012-12-12 01:34:34
您可以通过设置聊天窗口div的scrollTop属性来完成此操作。要滚动到div的底部,请将scrollTop设置为scrollHeight
var chatWindow = document.getElementById('chating');
chatWindow.scrollTop = chatWindow.scrollHeight;或者,使用jQuery:
var chatWindow = $("#chating");
chatWindow.scrollTop(chatWindow[0].scrollHeight);每次将聊天消息添加到div时,您都需要调用此函数。
https://stackoverflow.com/questions/13825327
复制相似问题