HTML DOM scrollLeft Property
Examples
Slide the scrollbar to get the distance from the element's scrollbar to the element's left or top:
Try it Yourself »
Definition and Usage
The scrollLeft property can read or set the distance from the element's scrollbar to the element's left.
Note that if the content direction of this element is rtl (right-to-left), the scrollbar will be at the far right (where the content starts), and the scrollLeft value is 0. In this case, when you drag the scrollbar from right to left, scrollLeft will change from 0 to a negative number.
On systems with display scaling, scrollLeft may be a decimal number.
Browser Support
| Property | |||||
|---|---|---|---|---|---|
| scrollLeft | Yes | Yes | Yes | Yes | Yes |
Syntax
Get the scrollLeft property value:
// 获取滚动条到元素左边的距离 var sLeft = element.scrollLeft
sLeft is an integer representing how many pixels the element's scrollbar is from the element's left.
Set the scrollLeft property value:
// 设置滚动条滚动了多少像素 element.scrollLeft = pixels
scrollLeft can be any integer, however:
- If the element cannot scroll (for example, the element has no overflow), then
scrollLeftits value is 0. - If you set
scrollLefta value less than 0, thenscrollLeftits value will become 0. - If you set
scrollLefta value greater than the maximum width of the element content, thenscrollLeftits value will be set to the element's maximum width.
Technical Details
| Return value: | Returns an integer representing the number of pixels the element's content has been horizontally scrolled. |
|---|
More Examples
Example
Set the element with ID "myDIV" to scroll 50 pixels horizontally and 10 pixels vertically:
Try it Yourself »
Example
Set the element with ID "myDIV" to scroll 50 pixels horizontally and 10 pixels vertically each time the button is clicked:
Try it Yourself »
Other Extensions
Element Object