Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript scroll bar, go to bottom

I'm currently coding a PHP and MYSQL chat. The chat messages are held in a div with the class '.chat'. It has a overflow:auto css element so it has a vertical scrollbar. I would like it so when you join the chat, it starts the scrollbar at the bottom.

I currently have the following code so far:

$(".chat").animate({ scrollTop: '7807897px' }, "fast");

This method doesn't always work, and can really become annoying. I know there must be an easier way out there, I'm just new to javascript and would really love some help :)

Thanks a lot

EDIT: My .chat div has a 80% height element. So the scroll by height doesn't seem to work for me

like image 278
Brandy Avatar asked Mar 31 '26 17:03

Brandy


1 Answers

Why not get the height of .chat ?

$(".chat").animate({
    scrollTop: $('.chat').height() 
}, "fast");

If your scrolling the page you can use

$(document).height();
like image 143
iConnor Avatar answered Apr 03 '26 07:04

iConnor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!