"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Using jQuery to detect how to scroll to or near the bottom of a web page

Using jQuery to detect how to scroll to or near the bottom of a web page

Posted on 2025-04-13
Browse:672

How Can I Detect When a User Scrolls to the Bottom (or Near the Bottom) of a Web Page Using jQuery?

Monitor Scrolling Depth for Precise Content Loading

In a pagination system where content loads upon reaching the bottom, determining when a user has scrolled to the extremity becomes crucial. jQuery provides an elegant solution to this challenge.

Method:

Execute the .scroll() event on the window object:

$(window).scroll(function() {
   // Calculate total scroll position and compare it to document height
   if($(window).scrollTop()   $(window).height() == $(document).height()) {
       alert("bottom!");
   }
});

This code sums the window's top scroll position (how far down it's scrolled) with the visible window's height, and compares this to the overall content height. When these values align, it signifies the user reaching the page bottom.

Near Bottom Detection:

For scenarios where you want to trigger an action before the actual bottom, modify the code as follows:

$(window).scroll(function() {
   // Check if user is within 100 pixels from the bottom
   if($(window).scrollTop()   $(window).height() > $(document).height() - 100) {
       alert("near bottom!");
   }
});

By adjusting the value after - 100 (e.g., to 50, 200), you can define the desired proximity threshold to the bottom.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3