English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM scrollTop property

HTML DOM Element Object

scrollTopThe property sets or returns the number of pixels the element content is scrolled vertically.

The scrollTop value of the element is the measurement from the top of the element to the topmost visible content. When the element's content does not generate a vertical scrollbar, its scrollTop value is 0.

UsingscrollLeftThe property sets or returns the number of pixels the element content is scrolled to the left (horizontally).

Syntax:

Return scrollTop property:

element.scrollTop

Set scrollTop property:

element.scrollTop = pixels
var elem = document.getElementById("container");
var x = elem.scrollLeft;
var y = elem.scrollTop;
Test and See‹/›

Browser compatibility

All browsers fully support the scrollTop property:

Property
scrollTopYesYesYesYesYes

Attribute Value

ValueDescription
pixelsSpecify the number of pixels the content of the element is scrolled vertically

Technical Details

Return Value:A number representing the number of pixels the content of the element has been scrolled vertically
DOM Version:CSS Object Model (CSSOM)

More Examples

Vertical scroll content of DIV50px:

document.getElementById(').container').scrollTop += 50;
Test and See‹/›

Vertical scroll content of BODY100 pixels:

var body = document.body;// For Safari
var html = document.documentElement; // Chrome, Firefox, IE and Opera
   
body.scrollTop += 100;
html.scrollTop += 100;
Test and See‹/›

Related References

HTML DOM Reference:scrollLeft property

HTML DOM Element Object