Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a CSS overflow scrollbar be controlled in Javascript?

I have a div with a class overflow: auto; Is there a way to control this scrollbar when it appears?

I have a ajax photo gallery (no page refresh) where some caption text is in a div, but the content is of varying length. If you scroll down in this div and then advance to the next slide, the scrollbar does not go back to the top. So, I was wondering if there was a way to control this scrollbar. Thanks.

like image 923
Stephen Avatar asked May 16 '11 19:05

Stephen


People also ask

How do I make my overflow scroll smoother?

You just need to add scroll-behavior: smooth; to your html style.

What is the difference between overflow scroll and auto?

The difference For that, you need overflow: auto , which lets a browser automatically determine if a scroll bar is needed — or not. So in short: overflow: scroll : Always show a scroll bar. overflow: auto : Show a scroll bar when needed.

How do you stop overflow scrolling in CSS?

To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element. This hides all content beyond the element's border. Alternatively, use overflow: visible to show content that extends beyond the borders of the container.


2 Answers

With jQuery, you can set the top of the scroll area like so:

$('#contents').scrollTop(0);

Here's a demo showing this in action->

Note that this is also possible without jQuery:

document.getElementById('contents').scrollTop = 0;

And a demo of that->

For both demos, press the Reset button to set the scroll bar back to the top.

like image 160
Ender Avatar answered Nov 07 '22 19:11

Ender


You can change the value of the property scrollTop of the <div> element.

See element.scrollTop on MDC docs

See element.scrollTop on MSDN

While scrollTop is not part of any standard, it is supported by all major browsers.

like image 4
Elian Ebbing Avatar answered Nov 07 '22 19:11

Elian Ebbing