Home >Web Front-end >JS Tutorial >How to Detect When a User Has Scrolled to the Bottom of a Div in jQuery?

How to Detect When a User Has Scrolled to the Bottom of a Div in jQuery?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-29 03:30:29935browse

How to Detect When a User Has Scrolled to the Bottom of a Div in jQuery?

Detecting End of Div Content Using jQuery

Question:

In a div element with overflowing content and auto overflow, how can we detect when the user has scrolled to the bottom of the div using jQuery?

jQuery Code for Bottom Scroll Detection:

<code class="jQuery">jQuery(function($) {
    $('#flux').on('scroll', function() {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
            // Event triggered when the end of the div is reached
            // Perform desired actions here
        }
    })
});</code>

Explanation:

  • Using scrollTop() to determine how far the user has scrolled within the div.
  • Adding innerHeight() to account for the visible portion of the div.
  • Comparing the sum to scrollHeight (the height of the content inside the div) to detect when the user has reached the bottom.

This approach provides a reliable way to trigger an event when the user scrolls to the end of the specific div, allowing for dynamic loading of content or any other desired functionality.

The above is the detailed content of How to Detect When a User Has Scrolled to the Bottom of a Div in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn