Use this lesson to learn Scrolling in JavaScript. It belongs to the UI Events chapter inside Browser: Document, Events, Interfaces, so it fits naturally into a complete JavaScript course from core language concepts to browser APIs.
The scroll event allows reacting to a page or element scrolling. There are quite a few good things we can do here.
For instance:
Show/hide additional controls or information depending on where in the document the user is.
Load more data when the user scrolls down till the end of the page.
Here’s a small function to show the current scroll:
We can’t prevent scrolling by using event.preventDefault() in onscroll listener, because it triggers after the scroll has already happened.
But we can prevent scrolling by event.preventDefault() on an event that causes the scroll, for instance keydown event for pageUp and pageDown.
If we add an event handler to these events and event.preventDefault() in it, then the scroll won’t start.
There are many ways to initiate a scroll, so it’s more reliable to use CSS, overflow property.
Here are few tasks that you can solve or look through to see applications of onscroll.
Keep learning JavaScript
Use this lesson as part of a complete JavaScript tutorial path. The topic connects with nearby lessons in the same chapter and supports practical web development skills. See the Learn UI Events in JavaScript topic hub for the full lesson cluster.
A function is a reusable block of JavaScript behavior that can receive arguments, return a value, and capture surrounding state.
Quick questions
What does this JavaScript lesson cover?
This lesson covers Scrolling as part of the UI Events chapter in the Browser: Document, Events, Interfaces section of the Learn JavaScript Online course.
Who should study Scrolling?
Study Scrolling if you want a structured JavaScript tutorial that connects this topic with surrounding lessons, examples, and browser-focused practice.
How does Scrolling fit into learning JavaScript?
Scrolling fits into the course sequence at lesson 3.6, helping you move from JavaScript fundamentals toward practical web development skills.