jQuery 自动滚动到底部 DIV

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

Auto Scroll to Bottom DIV

jquerycss

提问by Lcky Jhn Faderon

I have made chat system, but I had a problem with scrolling div for the messages that the scroll bar must be at the bottom as default.

我已经制作了聊天系统,但是我在滚动 div 时遇到了滚动条必须默认位于底部的消息的问题。

DIV

DIV

<div class="chat-body chat-scroll" id="chat-scroll"></div>

STYLE

风格

.chat-scroll{position: absolute; top:0px; bottom: 0px; overflow-y: auto;}

I use JQuery for submit message from the input form to reload the content of the #chat-scroll and the auto scrolling is okay for submit, but when at first load of the page, the scroll only stays at the middle of the div #chat-scroll. Unlike if I submit a message it will go to the bottom when I send message.

我使用 JQuery 从输入表单提交消息以重新加载 #chat-scroll 的内容,并且自动滚动可以提交,但是在第一次加载页面时,滚动只停留在 div #chat 的中间-滚动。与如果我提交消息不同,它会在我发送消息时转到底部。

JQuery For Scrolling

用于滚动的 JQuery

$('#chat-scroll').animate({
scrollTop: $('#chat-scroll').get(0).scrollHeight}, 2000);                   

回答by Manwal

When window load scroll your chat div at bottom using document.ready

当窗口加载时使用滚动底部的聊天 div document.ready

DEMO

演示

$(document).ready(function() {
    $('#chat-scroll').animate({
        scrollTop: $('#chat-scroll').get(0).scrollHeight
    }, 2000);
});

回答by Bhupinder kumar

var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;