Skip to main content

Scroll to bottom of div?


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?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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;

    ReplyDelete
  2. This is much easier if you're using jQuery:

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

    ReplyDelete
  3. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Wildcards in a hosts file

I want to setup my local development machine so that any requests for *.local are redirected to localhost . The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local , site2.local etc, and have them all resolve to localhost , while Apache serves a different site accordingly.