Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scroll programmatically a div with its own scrollbars?

Tags:

html

css

I have a HTML page with an internal DIV used for content. The internal DIV has its own scrollbars. I would like to automatically scroll to a certain position in the DIV.

How can I do this? (Note that I do NOT want to auto scroll the Window scrollbars - I already know how to do this)

A cross platform solution is needed

like image 728
yazz.com Avatar asked May 17 '12 11:05

yazz.com


People also ask

How do you make a div scrollable?

For vertical scrollable bar use the x and y axis. 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 add a horizontal scrollbar to a div?

For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.

How do I add a scrollbar in HTML?

Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.

What is scrollTop in JavaScript?

The Element. scrollTop property gets or sets the number of pixels that an element's content is scrolled vertically. An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content.


1 Answers

The div has a scrollTop property that you can set (and its pal, scrollLeft).

  • jsFiddle Demo

  • scrollTop on MDN

  • scrollLeft on MDN

like image 196
kapa Avatar answered Oct 11 '22 22:10

kapa