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)

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

https://reon777.com/2019/06/02/scrollTo/

Author

reon777

Posted on

2019-06-03

Updated on

2026-06-30

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×