[英]Javascript scroll bottom
I've built a small chat div where people can chat. 我已经建立了一个人们可以聊天的小聊天div。 It works with AJAX which pulls to records and places them.
它适用于AJAX,它可以记录并放置它们。
Everything is working fine except for a minor problem. 除了一个小问题,一切都很好。 Every time AJAX is called I want it to scroll the div down to the bottom.
每次调用AJAX时我都希望它将div向下滚动到底部。 (Latest chat messages are on the bottom)
(最新的聊天消息在底部)
However, I can't figure out how to track if a user is already scrolling inside the chat div themselves or not. 但是,我无法弄清楚如何跟踪用户是否已经在聊天div内部滚动。
I want to prevent the "auto scroll down" in case a user is active inside the chat. 如果用户在聊天中处于活动状态,我想阻止“自动向下滚动”。
How would I go around doing this? 我该怎么做呢?
HTML of the chat: 聊天的HTML:
<div id="chat">
<div id="chatoutput></div>
</div>
Javascript that makes it scroll to the bottom: 使其滚动到底部的Javascript:
jQuery("#chat").scrollTop($("#chat")[0].scrollHeight);
You can disable the auto scroll down if the user once scrolled manually: 如果用户手动滚动,您可以禁用自动向下滚动:
$("#chat").scroll(function() {
autoScrollEnabled = false;
});
You have to check autoScrollEnabled before scrolling down though. 在向下滚动之前,您必须检查autoScrollEnabled。
if (autoScrollEnabled) {
jQuery("#chat").scrollTop($("#chat")[0].scrollHeight);
}
You could also store a DateTime value to check when the user last scrolled and enable autoScroll if that was, say, 1 minute ago. 您还可以存储DateTime值以检查用户上次滚动的时间,并启用autoScroll(如果是1分钟前)。
Just use simple javascript
method. 只需使用简单的
javascript
方法。
$(document).ready(function () {
var objDiv = document.getElementById("chat");
objDiv.scrollTop = objDiv.scrollHeight;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.