views:

6391

answers:

3

I am creating an ajax chat in rails and I am 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?

+18  A: 

Here's what I use on my site (I didn't write it, I just found it somewhere since I don't know Javascript too well.)

var objDiv = document.getElementById("your_div");
objDiv.scrollTop = objDiv.scrollHeight;
yjerem
+3  A: 

If you use jQuery then may be you can take a look at this great plugin, it has a lot of options for scrolling, check it out here http://flesler.blogspot.com/2007/10/jqueryscrollto.html

Ahmad
That would be cool if you provide the way to do this and not only the link to the plugin.I personally failed to achieve this with the plugin, though I didn't tried hard, since the accepted solution worked for me.With this plugin I was trying: $(".messages").scrollTo("max"), $(".messages").scrollTo(999) to no avail.
dolzenko
+1  A: 

this is much easier if your using jquery.

$("#mydiv").scrollTop($("#mydiv")[0].scrollHeight);

andsien