Scrolling Overflowed DIVs with JavaScript

scrollHeight should be the total height of content. scrollTop specifies the pixel offset into that content to be displayed at the top of the element’s client area.

So you really want (still using jQuery):

$("#thediv").each( function() 
{
   // certain browsers have a bug such that scrollHeight is too small
   // when content does not fill the client area of the element
   var scrollHeight = Math.max(this.scrollHeight, this.clientHeight);
   this.scrollTop = scrollHeight - this.clientHeight;
});

…which will set the scroll offset to the last clientHeight worth of content.

Leave a Comment