AI Workshop: learn to build apps with AI →
UI APIs: How to get the scroll position of an element in JavaScript

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


How can you do that?

Once you have the element, you can inspect its scrollLeft and scrollTop properties.

The 0, 0 position is always found in the top left corner, so any scrolling is relative to that.

Example:

const container = document.querySelector('.container')
container.scrollTop
container.scrollLeft

Those properties are read/write, so you can also set the scroll position:

const container = document.querySelector('.container')
container.scrollTop = 1000
container.scrollLeft = 1000

Lessons in this unit:

0: Introduction
1: Clipboard API
2: Scrolling
3: Console API
4: Speech Synthesis API
5: ▶︎ How to get the scroll position of an element in JavaScript
6: How to make a page editable in the browser
7: Safari, warn before quitting