Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep overflow div scrolled to bottom unless user scrolls up

I have a div that is only 300 pixels big and I want it to when the page loads scroll to the bottom of the content. This div has content dynamically added to it and needs to stay scrolled all the way down. Now if the user decides to scroll up I don't want it to jump back to the bottom until the user scrolls all the way down again

Is it possible to have a div that will stay scrolled to the bottom unless the user scrolls up and when the user scrolls back to the bottom it needs to keep itself at the bottom even when new dynamic content is added. How would I go bout creating this.

like image 552
Robert E. McIntosh Avatar asked Sep 04 '13 12:09

Robert E. McIntosh


People also ask

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.

How do I make my Div overflow scroll?

Making a div vertically scrollable is easy by using CSS overflow property. There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;.

How do I automatically scroll down a page in CSS?

You can use . scrollIntoView() for this. It will bring a specific element into the viewport.


2 Answers

I was able to get this working with CSS only.

The trick is to use display: flex; and flex-direction: column-reverse;

The browser treats the bottom like its the top. Assuming the browsers you're targeting support flex-box, the only caveat is that the markup has to be in reverse order.

Here is a working example. https://codepen.io/jimbol/pen/YVJzBg

like image 186
Jim Hall Avatar answered Oct 14 '22 02:10

Jim Hall


This might help you:

var element = document.getElementById("yourDivID"); element.scrollTop = element.scrollHeight; 

[EDIT], to match the comment...

function updateScroll(){     var element = document.getElementById("yourDivID");     element.scrollTop = element.scrollHeight; } 

whenever content is added, call the function updateScroll(), or set a timer:

//once a second setInterval(updateScroll,1000); 

if you want to update ONLY if the user didn't move:

var scrolled = false; function updateScroll(){     if(!scrolled){         var element = document.getElementById("yourDivID");         element.scrollTop = element.scrollHeight;     } }  $("#yourDivID").on('scroll', function(){     scrolled=true; }); 
like image 38
Mr.Manhattan Avatar answered Oct 14 '22 01:10

Mr.Manhattan