Window.scrollBy()

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.

Window.scrollBy() メソッドは、ウィンドウ内の文書を指定された量だけスクロールさせます。

構文

js
scrollBy(x_coord, y_coord);
scrollBy(options);

引数

  • x_coord はスクロールさせたい水平方向のピクセル数です。
  • y_coord はスクロールさせたい垂直方向のピクセル数です。

- または -

options

以下の引数を含む辞書です。

top

ウィンドウまたは要素をスクロールする Y 軸方向のピクセル数を指定します。

left

ウィンドウまたは要素をスクロールする X 軸方向のピクセル数を指定します。

behavior

スクロールを滑らかにアニメーションさせるか (smooth)、瞬時に一回のジャンプで行うか (instant)、あるいはブラウザーに選択させるか (auto, 既定値) を指定することができます。

返値

なし (undefined)。

1 ページ分下スクロールさせるには、次のようにします。

js
window.scrollBy(0, window.innerHeight);

上スクロールさせるには、次のようにします。

js
window.scrollBy(0, -window.innerHeight);

options を使用します。

js
window.scrollBy({
  top: 100,
  left: 100,
  behavior: "smooth",
});

メモ

window.scrollBy() は特定の量だけスクロールしますが、 window.scroll() は文書内の絶対位置までスクロールします。 window.scrollByLines() および window.scrollByPages() も参照してください。

仕様書

Specification
CSSOM View Module
# dom-window-scrollby

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
scrollBy
options.behavior parameter
options.left parameter
options.top parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.