自动滚动到 div 底部
我有一个溢出设置为滚动的 div,它本质上是逐行流式传输文件中的数据。我想在流溢出时自动滚动到 div 的底部,但不使用“单击此处滚动到底部”按钮。
我已经知道 scrollTop =scrollHeight
解决方案,但是这需要客户端进行某种事件触发器。我不希望这个元素是交互式的;它应该自行滚动。
有什么办法可以实现这一点吗?
I have a div with overflow set to scroll which essentially streams data line by line off a file. I'd like to scroll automatically to the bottom of the div whenever the stream overflows, but without using a "Click here to scroll to bottom" button.
I already know of the scrollTop = scrollHeight
solution, but that requires some kind of event trigger on the client's side. I don't want this element to be interactive; it should scroll by itself.
Is there any way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很多scrollHeight 实现对我来说不起作用,offsetHeight 似乎可以解决问题。
可以肯定,scrollHeight 尝试将其移动到静态元素高度的底部,而不是可滚动区域的高度。
A lot of the scrollHeight implementations didn't work for me, offsetHeight seemed to do the trick.
Pretty sure that scrollHeight tries to move it to the bottom of the height of the static element, not the height of the scrollable area.
无法自动将元素滚动到底部。使用element.scrollTop = element.scrollHeight。
如果你不知道元素何时调整大小,你可以添加一个轮询器:
There's no way to automatically scroll an element to the bottom. Use
element.scrollTop = element.scrollHeight
.If you don't know when the element is going to resize, you could add a poller:
我的一些旧代码,其中包含 运行示例,该代码将保留在添加新内容时的底部,如果用户滚动它不会将其添加到底部。
Some old code of mine with a running example that will stay at the bottom when new content is added, if the user scrolls it will not more it to the bottom.