Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to scroll a <div>?

Status
Not open for further replies.

jaschulz

Programmer
Joined
May 20, 2005
Messages
89
Location
FR
How, given a <div style="overflow: auto; height: 200px;'>, can I control the vertical scrollbar programatically?

Thanks,

JAS
 
It varies a bit from browser to browser but look into the scrollTo method.
You can use scrollTop and scrollLeft to determine how many pixels you are already scrolled but the methods of determining how tall and wide the window is varies a lot browser to browser. If you have a fixed size div then you know how many pixels wide/tall it is and that makes things a lot easier.



Paranoid? ME?? Who wants to know????
 
Hey,

I checked out that link because I have the same problem with needing to scroll a div. I found a solution though:

Code:
<!-- JavaScript Part -->
<script>
function doScroll() {
   mydiv.scrollTop = 5000;
}
</script>

<!-- HTML Part -->
<div id="mydiv">lots of div data
<p>.<p>
Line 2
<p>.<p>
Line 3
<p>.<p>
Line 4
<p>.<p>
Line 5
<p>.<p>
Line 6
<p>.<p>
etc.
<p>.<p>
<p>.<p>
<p>.<p>
<p>.<p>
<p>.</div>

You might even be able to do += and -= on mydiv.scrollTop for smoother scrolling or to scroll in incremements. :-)
 
*edit

The div should probably have a scrollbar in my example ;-)

Code:
<div id="mydiv" style="height: 100px; overflow: scroll">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top