Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!

The Element.scrollLeft property gets or sets the number of pixels that an element's content is scrolled to the left.

Note that if the element's direction of the element is rtl (right-to-left) then scrollLeft is 0 when the scrollbar is at its rightmost position (at start of the scrolled content) and then increasingly negative as you scroll towards the end of the content.

Syntax

// Get the number of pixels scrolled
var sLeft = element.scrollLeft;

sLeft is an integer representing the number of pixels that element has been scrolled to the left.

// Set the number of pixels scrolled
element.scrollLeft = 10;

scrollLeft can be set to any integer value, however:

  • If the element can't be scrolled (e.g. it has no overflow), scrollLeft is set to 0.
  • If set to a value less than 0 (greater than 0 for right-to-left elements), scrollLeft is set to 0.
  • If set to a value greater than the maximum that the content can be scrolled, scrollLeft is set to the maximum.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        #container {
            border: 1px solid #ccc; height: 100px; overflow: scroll; width: 100px;
        }
        #content {
            background-color: #ccc; width: 250px;
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', function () {   
            var button = document.getElementById('slide');
            button.onclick = function () {
                document.getElementById('container').scrollLeft += 20;
            };
        }, false);
    </script>
</head>
<body>
    <div id="container">
        <div id="content">Lorem ipsum dolor sit amet.</div>
    </div>
    <button id="slide" type="button">Slide</button>
</body>
</html> 

Specifications

Specification Status Comment
CSS Object Model (CSSOM) View Module
The definition of 'scrollLeft' 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

MSDN: scrollLeft Property

文件標籤與貢獻者

 最近更新: jpmedley,