jsで特定要素をスクロールする方法

表題の通りです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// hogeクラスの最初の要素を取得する
const hoge = window.document.getElementsByClassName('hoge')[0]

// 現在の横スクロール位置を取得する
const now_x = hoge.scrollLeft
console.log('now_x: ' + now_x)

// 右に100pxスクロールする
hoge.scrollTo(now_x + 100, 0)

// おまけ
// 画面の横幅を取得する
const window_x = window.parent.screen.width
console.log('window_x: ' + window_x)
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×