Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

$('#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
                );
like image 273
CyanPrime Avatar asked Mar 11 '12 22:03

CyanPrime


People also ask

How do I change the scrollbar position?

We can use the CSS “::-webkit-scrollbar” property which is responsible for changing the shape, color, size, shade, shadow, etc. of the scroll bar. But, here the property which we will use is the direction property of CSS for changing the position of the scroll bar.

How do I keep my div scrolled to the bottom?

use css position top keep it at the bottom {position : relative; bottom:0;} . Remove the css property once the user has scroll.

How do you get the scroll position in HTML?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do I add a scrollbar down in HTML?

Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.


1 Answers

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") });
like image 195
CyanPrime Avatar answered Oct 16 '22 14:10

CyanPrime