DOM

Element.scrollBy() メソッド

 編集:2022.09.23 by T.A.

[Edge]。→

element の内容を指定量だけスクロールします。

構文
element.scrollBy(deltaX, deltaY)
Element.scrollBy(options)
element Element
deltaX 水平軸上の移動量をピクセル数で表す±数値。
deltaY 垂直軸上の移動量をピクセル数で表す±数値。
options オブジェクトで指定。
top y と同じです
left x と同じです
behavior 以下のうち1つを文字列で指定。
smooth スムース スクロール動作。
instant 即座にジャンプ動作になります。
auto ブラウザーに任せます。初期値。
戻り値

element.scrollBy(300,300);
options の使用例:
window.scrollBy({
  top: 100,
  left: 100,
  behavior: 'smooth'
});

HTML:

<DIV id=IdTest style="overflow:scroll; width:100px; height:100px; border:1px solid red; font-size:20px">aaaaaaaaaaaaaaaaaaaa<BR>
  <B>B要素</B>テキスト<BR>
  あ<BR>
  あ<BR>
  あ<BR>
  あ<BR>
  あ<BR>
  あ<BR>
  あ<BR>
  あ<BR>
</DIV>

JavaScript:

IdTest.scrollBy(20, 30); //要素をスクロール。
D=IdTest.scrollLeft +","+ IdTest.scrollTop;   //=[string]:20,30
IdTest.scrollBy(20, 30); //要素をスクロール。
D=IdTest.scrollLeft +","+ IdTest.scrollTop;   //=[string]:40,60
IdTest.scrollBy(-100, -100); //要素をスクロール。
D=IdTest.scrollLeft +","+ IdTest.scrollTop;   //=[string]:0,0

scrollLeftscrollTop

JavaScript: options での指定。

IdTest.scrollBy({top:200, left:200, behavior:'smooth'});

テスト