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
can you show us an example of what you mean?
I posted a quick example here:
http://kisdigital.wordpress.com/2009/11/14/example-animating-scroll-with-jquery/
Thank you!
It’s awesome!
Slight problem I’ve noticed…
When it’s at the bottom of a scrolled div, you can’t scroll back up!
I am looking into the issue and see if I can’t straighten it out.
I have fixed this bug and it is detailed in a new post.
http://kisdigital.wordpress.com/2010/03/10/using-jquery-to-scroll-to-the-bottom-of-a-div-revised/
Thanks for this example? This will be very handy for my browser game
thanks
Thanks Robert that was very helpful.
Thanks ,
I am Searching for this and it ends here.
Keep it up bro.
Noice, spent like 2-3 hours trying to figure this out through css, js, anything. Much appreciated
The .attr() must be replaced with .prop() as of jquery 1.6
Thanks for the heads up, Wilson. I will update the post.
Thanks for this!
that’s like a short and awesome tutorial . . Thanks man . .
Thanks for this…….
Thanks for this!
Helps me alot : )
$(“html body”).animate({ scrollTop: $(“html body”).prop(“scrollHeight”) }, 3000);
working like a charm for me : )
greetz
Thx – helped me out!
THIS works. Thank you! Perfect for scrolling a div with dynamic content added with append.