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
);
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.
use css position top keep it at the bottom {position : relative; bottom:0;} . Remove the css property once the user has scroll.
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.
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.
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") });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With