Home >Web Front-end >JS Tutorial >How Can I Detect When a User Has Scrolled to the Bottom of a Webpage?
Identifying User Scroll Position on a Web Page
Determining whether a user has scrolled to the bottom of a webpage is essential for executing specific actions, such as automatically updating the page. Here's how you can achieve this detection:
To begin, you need to register a scroll event listener on the window object:
window.onscroll = function(ev) {
Within this event handler, you can calculate the current scroll position and compare it to the height of the webpage:
if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) {
If the sum of window.innerHeight and window.scrollY is greater than or equal to document.body.offsetHeight, it implies that the user has reached the bottom of the page, triggering the actions you need to perform.
Example Implementation
For instance, to update the webpage with new content upon reaching the bottom, you could use the following code:
window.onscroll = function(ev) { if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) { // Load or generate new content to add to the bottom of the page } };
By employing this technique, you can effectively determine whether a user has scrolled to the end of a page and execute appropriate actions accordingly.
The above is the detailed content of How Can I Detect When a User Has Scrolled to the Bottom of a Webpage?. For more information, please follow other related articles on the PHP Chinese website!