javascript – 如何让我的滚动条自动修复到div的底部?

我正在制作一个网络聊天应用程序,我需要一些帮助.我将一些段落附加到div,并将溢出设置为auto.当包含聊天的div的内容已满时,它会创建一个滚动条,但仍保留在div的顶部.我希望滚动固定在底部,这样用户每次收到消息时都不需要滚动.我该怎么办?

解决方法:

Standard JavaScript solution

使用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>

以下是scrollTopgetClientRects的文档

上一篇:Windows下FFmpeg快速入门 <第二篇>


下一篇:测试利器:Postman 的入门及接口自动化