JavaScript - Detect scroll operation
A JavaScript code example for detecting scroll operations and getting scroll amount.
The HTML has an output element with id "output" to display scroll amount.
<p id="output">0</p>
An event triggered on scroll can be created with addEventListener()
using "scroll".
Scroll amount can be gotten with "pageYOffset".
The code example detects browser window scroll and gets the scroll amount.
window.addEventListener('scroll', () => {
document.querySelector('#output').innerHTML = window.pageYOffset;
});