JavaScript scroll


The onscroll event in JavaScript occurs when a scrollbar is used for an element. The event is fired when the user moves the scrollbar up or down. We can use the CSS overflow property for creating a scrollbar.

In HTML, we can use the onscroll attribute and assign a JavaScript function to it. We can also use the JavaScript's addEventListener() method and pass a scroll event to it for greater flexibility.

Example -

Example -