我正在制作一个网络聊天应用程序,我需要一些帮助.我将一些段落附加到div,并将溢出设置为auto.当包含聊天的div的内容已满时,它会创建一个滚动条,但仍保留在div的顶部.我希望滚动固定在底部,这样用户每次收到消息时都不需要滚动.我该怎么办?
解决方法:
使用scrollTop属性.结帐demo.
JavaScript代码
container.scrollTop = content.getClientRects()[0].height;
HTML
<div id="container">
<div id="content">Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!
</div>
</div>
以下是scrollTop和getClientRects的文档