即使更新后,jQuery 也会滚动到 Div 的底部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14918787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:05:41  来源:igfitidea点击:

jQuery Scroll to bottom of Div even after it updates?

jqueryscroll

提问by user115422

So I have a page with a chat-like div that fills up with text. It has a fixed height and width (defined by css).

所以我有一个页面,里面有一个类似聊天的 div,里面填满了文本。它具有固定的高度和宽度(由 css 定义)。

I need it to scroll to bottom upon every update, here's the JS I've used so far:

我需要它在每次更新时滚动到底部,这是我到目前为止使用的 JS:

$("#div1").animate({ scrollTop: $('#div1').height()}, 1000);

It works for a while and then suddenly stops... I think it might have something to do with my set height for the div? I'm not sure.

它工作了一段时间然后突然停止......我认为这可能与我为div设置的高度有关?我不知道。

So what can I do to ensure the div scrolls down to the bottom of it's content (the content is updated using a jQuery/AJAX function)

那么我该怎么做才能确保 div 向下滚动到其内容的底部(使用 jQuery/AJAX 函数更新内容)

Thanks in advance!

提前致谢!

EDIT: I've also tried $("#div1").animate({ scrollTop: $('#div1').height()}, 0);but it still stops scrolling at a certain point...

编辑:我也试过,$("#div1").animate({ scrollTop: $('#div1').height()}, 0);但它仍然在某个点停止滚动......

回答by Jai

Have you tried with .scrollHeight:

你有没有试过.scrollHeight

$("#div1").animate({ scrollTop: $("#div1")[0].scrollHeight}, 1000);

scollHeight documentation at MDN

MDN 上的 scollHeight 文档