Javascript 如何确定滚动高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7381817/
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-24 02:03:35  来源:igfitidea点击:

How do I determine scrollHeight?

javascriptjquery

提问by Brian

How do I determine scrollHeight of a division use css overflow:auto?

如何使用 css overflow:auto 确定分区的 scrollHeight?

I've tried:

我试过了:

$('test').scrollHeight();
$('test').height(); but that just returns the size of the div not all the content

Ultimately, I'm trying to create a chat and always have the scroll bar to the current message on the screen.

最终,我正在尝试创建聊天并始终在屏幕上显示当前消息的滚动条。

So I was thinking something like the following:

所以我在想类似以下的事情:

var test = $('test').height();
$('test').scrollTop(test);

Thank you,

谢谢,

Brian

布赖恩

采纳答案by Dennis

scrollHeightis a regular javascript property so you don't need jQuery.

scrollHeight是一个常规的 javascript 属性,所以你不需要 jQuery。

var test = document.getElementById("foo").scrollHeight;

回答by Anmol Saraf

Correct ways in jQuery are -

jQuery 中的正确方法是 -

  • $('#test').prop('scrollHeight')OR
  • $('#test')[0].scrollHeightOR
  • $('#test').get(0).scrollHeight
  • $('#test').prop('scrollHeight')或者
  • $('#test')[0].scrollHeight或者
  • $('#test').get(0).scrollHeight

Hope it helps.

希望能帮助到你。