Element: scrollLeft プロパティ
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Element.scrollLeft
プロパティは、要素の内容が左端からスクロールするピクセル数を取得または設定します。
要素の direction
が rtl
(right-to-left、右書き)の場合、scrollLeft
が 0
のときにスクロールバーが右端の位置(スクロールする内容の開始位置)にあり、コンテンツの末尾にスクロールするにつれて、負の数が増加していきます。
scrollLeft
は任意の整数値で設定することができます。しかし、
- 要素がスクロールできない場合(例えば、はみ出した部分がない場合)、
scrollLeft
は0
に設定されます。 - 指定された値が
0
より小さかったら(右書きの要素では0
より大きかったら)、scrollLeft
は0
に設定されます。 - 指定された値が、コンテンツがスクロールできる最大値を超えていたら、
scrollLeft
は最大値に設定されます。
警告:
画面拡大を使用するシステムでは、 scrollLeft
が小数になることがあります。
値
数値です。
例
HTML
html
<div id="container">
<div id="content">ボタンをクリックすると右へスライドします。</div>
</div>
<button id="slide" type="button">右へスライド</button>
CSS
css
#container {
width: 100px;
height: 100px;
border: 1px solid #ccc;
overflow-x: scroll;
}
#content {
width: 250px;
background-color: #ccc;
}
JavaScript
js
const button = document.getElementById("slide");
button.onclick = () => {
document.getElementById("container").scrollLeft += 20;
};
結果
仕様書
Specification |
---|
CSSOM View Module # dom-element-scrollleft |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
scrollLeft |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.