Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-scroll to end of div when data is added? [duplicate]

I have a table inside of a div with the following style:

#data {     overflow-x:hidden;     overflow-y:visible;     height:500px; } 

This displays a vertical scroll bar once enough data is added to the table. However, when new data is added I would like the div element to auto-scroll to the bottom.

What JavaScript code could I use to do so? I am open to options in JQuery if necessary but would prefer a JavaScript solution.

like image 553
Drew Galbraith Avatar asked Sep 05 '11 04:09

Drew Galbraith


People also ask

How do I auto scroll to the end of a div?

To auto-scroll to end of div when data is added with JavaScript, we can set the scrollTop of the div to the scrollHeight of the div. window. setInterval(() => { const elem = document. getElementById("data"); elem.

How do I auto scroll to the bottom in HTML?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

How do you scroll to the bottom of a page automatically?

To use you just need to press CTRL+ Left click of your mouse and drag the mouse a bit in the direction you want to scroll the page. For example, if you want to scroll up to the page automatically, click CTRL+ left click and slightly move your mouse upwards, the tool will start scrolling up the page.


1 Answers

If you don't know when data will be added to #data, you could set an interval to update the element's scrollTop to its scrollHeight every couple of seconds. If you are controlling when data is added, just call the internal of the following function after the data has been added.

window.setInterval(function() {   var elem = document.getElementById('data');   elem.scrollTop = elem.scrollHeight; }, 5000); 
like image 152
VNO Avatar answered Sep 20 '22 02:09

VNO