HTML DOM scrollLeft Property
Example
Get the number of pixels the content of a <div> element is scrolled horizontally and vertically:
var x = elmnt.scrollLeft;
var y = elmnt.scrollTop;
More "Try it Yourself" examples below.
Definition and Usage
The scrollLeft property sets or returns the number of pixels an element's content is scrolled horizontally.
Tip: Use the scrollTop property to set or return the number of pixels an element's content is scrolled vertically.
Tip: To add scrollbars to an element, use the CSS overflow property.
Browser Support
Property | |||||
---|---|---|---|---|---|
scrollLeft | Yes | Yes | Yes | Yes | Yes |
Syntax
Return the scrollLeft property:
Set the scrollLeft property:
Property Values
Value | Description |
---|---|
pixels |
Specifies the number of pixels the element's content is scrolled
horizontally. Special notes:
|
Technical Details
Return Value: | A Number, representing the number of pixels that the element's content has been scrolled horizontally |
---|
More Examples
Example
Scroll the contents of a <div> element TO 50 pixels horizontally and 10 pixels vertically:
elmnt.scrollLeft = 50;
elmnt.scrollTop = 10;
Example
Scroll the contents of a <div> element BY 50 pixels horizontally and 10 pixels vertically:
elmnt.scrollLeft += 50;
elmnt.scrollTop += 10;
Example
Scroll the contents of <body> by 30 pixels horizontally and 10 pixels vertically:
var html = document.documentElement; // Firefox and IE places the overflow at the <html> level, unless else is specified. Therefore, we use the documentElement property for these two browsers
body.scrollLeft += 30;
body.scrollTop += 10;
html.scrollLeft += 30;
html.scrollTop += 10;