ウィンドウscrollBy()
例
ドキュメントを水平方向に100pxスクロールします。
window.scrollBy(100, 0);
ドキュメントを垂直方向に100ピクセルスクロールします。
window.scrollBy(0, 100);
以下のその他の例。
定義と使用法
このscrollBy()
メソッドは、指定されたピクセル数だけドキュメントをスクロールします。
構文
window.scrollBy(x, y)
あるいは単に:
scrollBy(x, y)
パラメーター
Parameter | Description |
x | Required. Number of pixels to scroll (horizontally). Positive values scroll to the right, negative values to the left. |
y | Required. Number ofpixels to scroll (vertically). Positive values scroll down, negative values scroll up. |
戻り値
なし |
その他の例
ドキュメントを上下にスクロールします。
<button onclick="scrollWin(0, 50)">Scroll down</button>
<button onclick="scrollWin(0, -50)">Scroll up</button>
<script>
function scrollWin(x, y) {
window.scrollBy(x, y);
}
</script>
ドキュメントを左右にスクロールします。
<button onclick="scrollWin(100, 0)">Scroll right</button>
<button onclick="scrollWin(-100, 0)">Scroll left</button>
<script>
function scrollWin(x, y) {
window.scrollBy(x, y);
}
</script>
ブラウザのサポート
scrollBy()
すべてのブラウザでサポートされています:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |