简体   繁体   English

如何在底部创建一个html滚动条?

[英]how can I make a html scrollbar start at the bottom?

I'm trying to make a scrollable area for a chatbox, but my scrollbar starts at the top and not the bottom. 我正在尝试为聊天框创建一个可滚动区域,但我的滚动条从顶部开始而不是从底部开始。 This means you see all the first messages, but not all the new ones until you scroll down. 这意味着在向下滚动之前,您会看到所有第一条消息,但不会显示所有新消息。 This chatbox is going to get a lot of messages, so the bar needs to start at the bottom. 这个聊天框会收到很多消息,所以条形图需要从底部开始。

This is what I got so far in JQuery, but it's not working 这是我到目前为止在JQuery中得到的,但它不起作用

$('#chatbox').each(function(){
                $(this).scrollTop($(this).prop('scrollHeight'));
            });

So how do I get a scrollbar to stay at the bottom of the scroll? 那么如何让滚动条保持在滚动的底部?

Edit: Now using this code. 编辑:现在使用此代码。 it goes to around the middle, but not the bottom. 它绕到中间,但不是底部。

$('#chatbox').animate({ 
                   scrollTop: $('#chatbox').height()}, 0
                );

Okay I figured it out. 好吧我明白了。 This is the code that got it working: 这是让它工作的代码:

$('#chatbox').animate({ 
                   scrollTop: $("#chatbox").prop("scrollHeight")}, 0
                );

Or for non-animated: 或者对于非动画:

            $("#chatbox").prop({ scrollTop: $("#chatbox").prop("scrollHeight") });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM