I am creating a chat using ajax requests in rails and I'm trying to get a div to scroll to the bottom without much luck.
I am wrapping everything in this div:
#scroll {
height:400px;
overflow:scroll;
}
Is there a way to keep it scrolled to the bottom by default using JS?
Is there a way to keep it scrolled to the bottom after an ajax request?
This will let you scroll all the way down regards the document height
If you don't want to rely on
scrollHeight
, the following code helps:using jQuery animate:
I know this is an old question, but none of these solutions worked out for me. I ended up using offset().top to get the desired results. Here's what I used to gently scroll the screen down to the last message in my chat application:
I hope this helps someone else.
A very simple method to this is to set the
scroll to
to the height of the div.Newer method :