Material Box

Material Box

WEB Design & Material Images

JavaScript - Detect scroll operation

JavaScript

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;
});

JavaScript - Detect scroll operation

TitleJavaScript - Detect scroll operation

CategoryJavaScript

Created

Update

AuthorYousuke.U