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.
- event: This parameter is required. It specifies one or more event(s) or namespaces to attach to the selected elements.
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= </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= </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:
Recommended Posts:
- How to Change Image Dynamically when User Scrolls using JavaScript ?
- Change Image Dynamically when User Scrolls Down | Set 2
- How to overlay one div over another div using CSS
- How to copy the content of a div into another div using jQuery ?
- How to center a div within another div?
- How to clear all div's content inside a parent div ?
- How to detect the change in DIV's dimension ?
- How to detect and change the content/style of a div using jQuery ?
- How to align content of a div to the bottom using CSS ?
- How to position a div at the bottom of its container using CSS?
- How to set div with left image and button at bottom using bootstrap?
- How to hide a div when the user clicks outside of it using jQuery?
- How to detect the user's device using jQuery ?
- Detect the Operating System of User using JavaScript
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- Making a div vertically scrollable using CSS
- How to set the div height to auto-adjust to background size?
- Div Tag | HTML
- How to horizontally center a div using CSS?
- How to auto-resize an image to fit a div container using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


