[英]always scroll to a scrollable div to bottom by always scrolling a very large number
I have a scrollable div which would load conversation dynamically through ajax, and i would like to scroll the div always to the bottom with fresh conversation coming in. 我有一个可滚动的div,它将通过ajax动态加载对话,并且我想将div始终滚动到底部,并加入新的对话。
can I simply use 我可以简单地使用
$(".conversation").scrollTop(1000000000);
to fulfill this task? 完成这项任务? will it has any disadvantages even though what you actually need is 100. I wonder whenever it scrolls to the bottom, and it won't bother to scroll another 9999999900? 即使您实际需要的是100,它也会有什么缺点吗?我想知道它何时滚动到底部,又不会打扰另一个9999999900?
According to https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollTop : 根据https://developer.mozilla.org/zh-CN/docs/Web/API/Element.scrollTop :
"If set to a value greater than the maximum that the content can be scrolled, scrollTop is set to the maximum." “如果设置的值大于可滚动内容的最大值,则scrollTop将设置为最大值。”
So shouldn't be a problem. 所以应该不成问题。
Just set the div's scrollTop
to its scrollHeight
: 只需将div的scrollTop
设置为其scrollHeight
:
$(".conversation").scrollTop($(".conversation")[0].scrollHeight);
This way you could have a div that is 1000000001px high without having to change code and it looks a little better than a random large number. 这样,您可以将div高1000000001px,而无需更改代码,并且它看起来比随机的大数字好一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.