Scrolling Overflowed DIVs with JavaScript

67,024

Solution 1

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.

Solution 2

scrollIntoView

The scrollIntoView method scrolls the element into view.

Solution 3

Using a loop to iterate over a jQuery of one element is quite inefficient. When selecting an ID, you can just retrieve the first and unique element of the jQuery using get() or the [] notation.

var div = $("#thediv")[0];

// 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(div.scrollHeight, div.clientHeight);
div.scrollTop = scrollHeight - div.clientHeight;

Solution 4

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

Solution 5

It can be done in plain JS. The trick is to set scrollTop to a value equal or greater than the total height of the element (scrollHeight):

const theDiv = document.querySelector('#thediv');
theDiv.scrollTop = Math.pow(10, 10);

From MDN:

If set to a value greater than the maximum available for the element, scrollTop settles itself to the maximum value.

While the value of Math.pow(10, 10) did the trick using a too high value like Infintiy or Number.MAX_VALUE will reset scrollTop to 0 (Firefox 66).

Share:
67,024
Bob Somers
Author by

Bob Somers

Robotics Engineer at a stealth mode startup.

Updated on April 03, 2020

Comments

  • Bob Somers
    Bob Somers about 4 years

    I've got a div that uses overflow:auto to keep the contents inside the div as it is resized and dragged around the page. I'm using some ajax to retrieve lines of text from the server, then append them to the end of the div, so the content is growing downwards. Every time this happens, I'd like to use JS to scroll the div to the bottom so the most recently added content is visible, similar to the way a chat room or command line console would work.

    So far I've been using this snippet to do it (I'm also using jQuery, hence the $() function):

    $("#thediv").scrollTop = $("#thediv").scrollHeight;
    

    However it's been giving me inconsistent results. Sometimes it works, sometimes not, and it completely ceases to work if the user ever resizes the div or moves the scroll bar manually.

    The target browser is Firefox 3, and it's being deployed in a controlled environment so it doesn't need to work in IE at all.

    Any ideas guys? This one's got me stumped. Thanks!

  • Kato
    Kato over 12 years
    Not that inefficient, since it will only be iterating once. However, if you were going to run the each a few thousand times in a second, you'd certainly notice the overhead of creating a function scope each time vs using [0] ;)