Home > jQuery > Using jQuery to scroll to the bottom of a DIV

Using jQuery to scroll to the bottom of a DIV


I have been working on a little AJAX application using jQuery and ColdFusion using JSON to communicate with the page through components. One of the first things I needed to do was to maximize scroll position of a DIV element each time it was updated. Digging around I finally found what I was looking for.

For my benefit, and for anyone else who needs it, assuming you have a div on the page like so:

<div id="myDiv" style="width: 500px; height: 300px; overflow: auto;"></div>

Using jQuery, each time it is updated we could fix the scroll position:

$("#myDiv").attr({ scrollTop: $("#myDiv").attr("scrollHeight") });

And if you would prefer an animated transition, ljpw suggests using:

$("#myDiv").animate({ scrollTop: $("#myDiv").attr("scrollHeight") }, 3000);

You can see a demo here:http://demos.kisdigital.com/index.cfm/demos/scroll

EDIT: Post has been revised
http://kisdigital.wordpress.com/2010/03/10/using-jquery-to-scroll-to-the-bottom-of-a-div-revised/

Edit 2: jQuery 1.6+ notes
Wilson pointed out in the blog comments that the script should be modified for newer versions of jQuery:

The .attr() must be replaced with .prop() as of jquery 1.6
Categories: jQuery
  1. richard
    November 14, 2009 at 5:18 pm | #1

    can you show us an example of what you mean?

  2. January 2, 2010 at 7:31 pm | #3

    Thank you!
    It’s awesome! :D

  3. Nick
    March 10, 2010 at 6:57 am | #4

    Slight problem I’ve noticed…

    When it’s at the bottom of a scrolled div, you can’t scroll back up! :(

  4. April 10, 2010 at 1:40 pm | #7

    Thanks for this example? This will be very handy for my browser game :-)

  5. mrsemsim
    December 13, 2010 at 9:26 am | #9

    Thanks Robert that was very helpful.

  6. Kunal
    April 12, 2011 at 1:44 pm | #10

    Thanks ,

    I am Searching for this and it ends here.
    Keep it up bro.

  7. The Ultimation
    May 16, 2011 at 6:15 pm | #11

    Noice, spent like 2-3 hours trying to figure this out through css, js, anything. Much appreciated

  8. Wilson
    June 3, 2011 at 6:39 am | #12

    The .attr() must be replaced with .prop() as of jquery 1.6

  9. Curious Nepali
    July 10, 2011 at 3:34 pm | #15

    that’s like a short and awesome tutorial . . Thanks man . .

  10. September 11, 2011 at 12:00 pm | #16

    Thanks for this…….

  11. September 15, 2011 at 7:51 pm | #17

    Thanks for this!
    Helps me alot : )

    $(“html body”).animate({ scrollTop: $(“html body”).prop(“scrollHeight”) }, 3000);

    working like a charm for me : )

    greetz

  12. March 23, 2012 at 12:56 am | #18

    Thx – helped me out!

  13. Mowgli
    May 27, 2012 at 7:32 pm | #19

    THIS works. Thank you! Perfect for scrolling a div with dynamic content added with append.

  1. March 10, 2010 at 12:14 pm | #1
  2. January 3, 2011 at 8:47 am | #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 364 other followers