Element.scrollTop
The Element.scrollTop
property gets or sets the number of pixels that the content of an element is scrolled upward. An element's scrollTop
is a measurement of the distance of an element's top to its topmost visible content. When an element content does not generate a vertical scrollbar, then its scrollTop
value defaults to 0
.
Syntax
// Get the number of pixels scrolled var <var> intElemScrollTop</var> = someElement.scrollTop;
After running this code, intElemScrollTop
is an integer corresponding to number of pixels that the element
's content has been scrolled upward.
// Set the number of pixels scrolled <var>element</var>.scrollTop = <var>intValue</var>;
scrollTop
can be set to any integer value, with certain caveats:
- If the element can't be scrolled (e.g. it has no overflow or if the element is non-scrollable),
scrollTop
is set to0
. - If set to a value less than
0
,scrollTop
is set to0
. - If set to a value greater than the maximum that the content can be scrolled,
scrollTop
is set to the maximum.
Example
padding-top
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
padding-bottom
Specifications
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) View Module The definition of 'scrollTop' in that specification. |
Working Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | ? | ? | ? | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | ? | ? | ? | ? | ? | (Yes) |
References
- W3C Draft CSSOM View Module
- MSDN's scrollTop definition
- MSDN's Measuring Element Dimension and Location
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/element/scrolltop