How to detect when user scrolls to the bottom of a div ?

03 Aug, 2021
The task is to detect the bottom of the <div> element when user scrolls to the bottom using JQuery. Here are few methods discussed.
jQuery on() Method:This method adds one or more event handlers for the selected elements and child elements.Syntax:$(selector).on(event, childSelector, data, function, map)
Parameters:event: This parameter is required. It specifies one or more event(s) or namespaces to attach to the selected elements.In the case of multiple event values, those are separated by space. The event must be valid.childSelector: This parameter is optional. It specifies that the event handler should only be attached to the defined child elements.data: This parameter is optional. It specifies additional data to pass to the function.function: This parameter is required. It specifies the function to run when the event occurs.map: It specifies an event map ({event:func(), event:func(), ...}) having one or more event to add to the selected elements, and functions to run when the events happens.
$(selector).on(event, childSelector, data, function, map)

Parameters:
event: This parameter is required. It specifies one or more event(s) or namespaces to attach to the selected elements.In the case of multiple event values, those are separated by space. The event must be valid.
childSelector: This parameter is optional. It specifies that the event handler should only be attached to the defined child elements.
data: This parameter is optional. It specifies additional data to pass to the function.
function: This parameter is required. It specifies the function to run when the event occurs.
map: It specifies an event map ({event:func(), event:func(), ...}) having one or more event to add to the selected elements, and functions to run when the events happens.
Example 1: This example alerts You reached the end of the DIV when the user scrolls to the bottom of the div element with class = div.
<!DOCTYPE HTML><html>  <head>    <title>        JQuery       | Detecting when user scrolls to bottom of div.    </title></head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>  <body style="text-align:center;"       id="body">    <h1 style="color:green;">             GeeksForGeeks         </h1>    <p id="GFG_UP"        style="font-size: 17px;              font-weight: bold;">    </p>    <div class="div">        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>        <h1>GeeksforGeeks</h1>    </div>    <script>        $('#GFG_UP').text(          'Scroll till bottom to get alert!');                $(window).on('scroll', function() {            if ($(window).scrollTop() >= $(              '.div').offset().top + $('.div').                outerHeight() - window.innerHeight) {                                alert('You reached the end of the DIV');            }        });    </script></body>  </html>
Output:
Before reaching the bottom:
After reaching the bottom:
Example 2: This example alerts End of DIV is reached! when the user scrolls to the bottom of the div element with class = div.
<!DOCTYPE HTML><html>  <head>    <title>        JQuery       | Detecting when user scrolls to bottom of div.    </title></head><script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>  <body style="text-align:center;" id="body">    <h1 style="color:green;">             GeeksForGeeks         </h1>    <p id="GFG_UP"       style="font-size: 17px;              font-weight: bold;">    </p>    <center>        <div class="div" style="width:200px;                                height:150px;                                overflow:auto;">            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>            <h1>GeeksforGeeks</h1>        </div>    </center>    <script>        $('#GFG_UP').text('Scroll till bottom to get alert!');        jQuery(function($) {            $('.div').on('scroll', function() {                if ($(this).scrollTop() +                    $(this).innerHeight() >=                     $(this)[0].scrollHeight) {                                        alert('End of DIV is reached!');                }            });        });    </script></body>  </html>
Output:
Before reaching the bottom:
After reaching the bottom:
jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it’s philosophy of “Write less, do more”.You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
jQuery-Misc
JavaScript
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to add options to a select element using jQuery?
jQuery | children() with Examples