I have a question about the scrollTop
and scrollLeft
properties. I would like to set these properties in my div without using JavaScript (jQuery). Is this possible?
My problem is, I have a program which interprets the HTML code and the divs which I scrolled have the value zero when the program interpret the code. The program can't read the scrollTop
or scrollLeft
value. Exists a HTML-tag like:
<div scrollLeft="300" scrollTop="200"></div>
To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.
In the Gantt component, you can set the vertical scroller position dynamically by clicking the custom button using the setScrollTop method.
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.
If you want to scroll the current document to a particular place, the value of HREF should be the name of the anchor to which to scroll, preceded by the # sign. If you want to open another document at an anchor, give the URL for the document, followed by #, followed by the name of the anchor.
I'm afraid that this is not possible using CSS/HTML only.
I created a solution that works in the following browsers:
It's really a bit hacky, so see whether you would use it or not. :)
A little explanation
I used the HTML5 attribute autofocus
on an <input>
-field. As this will focus the input, it has to get it into the viewport. Therefor it will scroll to the given position. To get rid of the highlighted outline and to not see the input at all, you have to set some styles. But this still forced Safari to have one blinking pixel, so I did the trick with the span, that acts like an overlay. Note that you can't simply use display: none
as this won't trigger the autofocus (only tested this in Safari).
Demo
The demo will run in Safari and Chrome only. IE and Firefox seem to not fire autofocus in an <iframe>
.
div.outer { height: 100px; width: 100px; overflow: auto; } div.inner { position: relative; height: 500px; width: 500px; } div.inner>input { width: 1px; height: 1px; position: absolute; z-index: 0; top: 300px; left: 200px; border: 0; outline: 0; } div.inner>span { width: 1px; height: 1px; position: absolute; z-index: 1; top: 300px; left: 200px; background: white; }
<div class="outer"> <div class="inner"> <input type="text" autofocus></input> <span></span> </div> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With