Get the number of pixels the content of a <div> element is scrolled horizontally and vertically:
More "Try it Yourself" examples below.
The scrollTop property sets or returns the number of pixels an element's content is scrolled vertically.
Tip: Use the scrollLeft property to set or return the number of pixels an element's content is scrolled horizontally.
Tip: To add scrollbars to an element, use the CSS overflow property.
Property | |||||
---|---|---|---|---|---|
scrollTop | Yes | Yes | Yes | Yes | Yes |
Return the scrollTop property:
Set the scrollTop property:
Value | Description |
---|---|
pixels |
Specifies the number of pixels the element's content is scrolled
vertically. Special notes:
|
Return Value: | A Number, representing the number of pixels that the element's content has been scrolled vertically |
---|
Scroll the contents of a <div> element TO 50 pixels horizontally and 10 pixels vertically:
Scroll the contents of a <div> element BY 50 pixels horizontally and 10 pixels vertically:
Scroll the contents of <body> by 30 pixels horizontally and 10 pixels vertically: