Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I auto-scroll as content is added to a div?

I have a div of fixed dimensions into which some JavaScript functions will be placing text over time. When the amount of text exceeds the height of the box, a scrollbar appears thanks to overflow:scroll.

The new problem is that the view into the div stays at the same place as more content appears. What I mean to say is that it stays scrolled wherever it is as more content appears beneath, hidden unless you manually scroll down. I want to make it automatically scroll to the bottom as new content appears so that the user naturally sees what appeared most recently instead of what's oldest.

Ideas?

like image 470
temporary_user_name Avatar asked May 14 '12 19:05

temporary_user_name


People also ask

How do I make div scroll automatically?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I make scrolling automatically?

To use auto-scrolling, click the scroll wheel by pushing in on the wheel on a blank or empty portion of the screen. Once clicked, one of the three scrolling cursor icons (shown to the right) are shown, depending on the program you're using.

How do I scroll content inside a div?

To fit all the text inside the div, the single-direction scrolling method will be used. You can apply it by placing the overflow-y:scroll in the id growth inside the style tag. Notice the scroll bar on the right side of the div .

Can you enable auto scroll?

Google Chrome: Use the AutoScroll Extension To install AutoScroll, navigate to the extension page and click the Add to Chrome button. When complete, you should see a multidirectional crosshair appear in your extensions bar, which is to the right of your address bar.


1 Answers

You can use scrollTop method after each text addition:

$("div").scrollTop($("div").children().height());

Use inner block to get the true height.

DEMO: http://jsfiddle.net/eyY5k/1/

like image 81
VisioN Avatar answered Sep 23 '22 16:09

VisioN