Skip to content

Instantly share code, notes, and snippets.

@toshimaru
Last active March 25, 2026 00:25
Show Gist options
  • Select an option

  • Save toshimaru/6102647 to your computer and use it in GitHub Desktop.

Select an option

Save toshimaru/6102647 to your computer and use it in GitHub Desktop.
Detect the scrolling to bottom of the page using jQuery.
$(window).on("scroll", function() {
var scrollHeight = $(document).height();
var scrollPosition = $(window).height() + $(window).scrollTop();
if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
// when scroll to bottom of the page
}
});
@OneAdder

OneAdder commented Sep 1, 2018

Copy link
Copy Markdown

Thanks a lot!

@mamster12

Copy link
Copy Markdown

works perfectly on desktop and mobile version!

@doganoo

doganoo commented Nov 24, 2018

Copy link
Copy Markdown

works perfect, thank you!

@Thierkaz

Thierkaz commented Dec 2, 2018

Copy link
Copy Markdown

Hi,
is not necessary to have this operation :
if ((scrollHeight - scrollPosition) / scrollHeight === 0) because it's mean that (scrollHeight - scrollPosition) === 0.
So it's better and simpler to write this :
if ( scrollHeight - scrollPosition === 0 ) { }
Because that' way you easily change your value limit like this
if( scrollHeight - scrollPosition < 20 ) { }
if you want somethings happen when the user is 20 pixels about to reach the page bottom.

@mannajstu

Copy link
Copy Markdown

awesome

@enetarch

enetarch commented Jan 11, 2019

Copy link
Copy Markdown

For those who find that this script doesn't work, check this property in your HMTL file .. < !DOCTYPE html >. As I found out, this can cause the window and document to report the same value, which breaks this script. This happens specifically in Chrome. And, if the tag is present, but specifies a URL after it, it may be necessary to remove the URL, or change it.

Welcome to the sauce that is the never ending problem of a Universal Interface.

jquery $(window).height() == $(document).height()
https://stackoverflow.com/questions/12103208/jquery-window-height-is-returning-the-document-height

@wackyapps

Copy link
Copy Markdown

Does this work in mobile browser with responsive layout bootstrap?

@Sathya-Kasithangam

Copy link
Copy Markdown

Is it possible to pause between scrolls?

@dovidezra

Copy link
Copy Markdown

Does Not work on mobile

@maidulcu

Copy link
Copy Markdown

Will this work on mobile on tap?

@Asad2175

Asad2175 commented Jul 1, 2019

Copy link
Copy Markdown

but it does not works on the popup

@Risyandi

Copy link
Copy Markdown

awesome

@f22hd

f22hd commented Aug 1, 2019

Copy link
Copy Markdown

Awesome.

@alierdogan7

Copy link
Copy Markdown

Google Chrome gives the full height of the page if you call $(window).height()

Instead, use window.innerHeight to retrieve the height of your window.
Necessary check should be:

    if($(window).scrollTop() + window.innerHeight > $(document).height() - 50) {
        console.log("reached bottom!");
    }

@hillbilly300

Copy link
Copy Markdown

don't work on mobile devices.

@slidenerd

slidenerd commented Sep 28, 2019

Copy link
Copy Markdown

NON JQUERY version

 let listContainer = document.getElementById('news_list_container');
    listContainer.addEventListener('scroll', function(e) {
      // https://stackoverflow.com/questions/876115/how-can-i-determine-if-a-div-is-scrolled-to-the-bottom
//       https://stackoverflow.com/questions/6271237/detecting-when-user-scrolls-to-bottom-of-div-with-jquery, 12px buffer to make this work properly
      if (listContainer.scrollTop >= (listContainer.scrollHeight - listContainer.offsetHeight - 12)) {
        console.log('we are at the bottom now', listContainer.scrollTop, listContainer.scrollHeight - listContainer.offsetHeight) ;
      }
    });

@egocarib

Copy link
Copy Markdown

@areghunanyan - worked perfectly!

@stanley123george

Copy link
Copy Markdown

I had the same issue, but none of these solutions helped me. For whom it is not not working just try this code and you are done

       $(window).on("scroll", function() {
            if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
                // ok
            }
        });

This code works perfect!

How to modify this code to trigger event 100px before bottom?
Is it possible?

@hibadattebayo

Copy link
Copy Markdown

@stanley123george i am wondering the same, but then about whether it is possible to trigger the event for a certain div

@DoHue97

DoHue97 commented Dec 3, 2019

Copy link
Copy Markdown

don't work on mobile devices.

I also had the same problem with you. On mobile don't work. Can anyone help me? Please!!!

@kamleshwebtech

Copy link
Copy Markdown

How to detect when user scrolls to the bottom of a div ? - This worked for me.

https://www.geeksforgeeks.org/how-to-detect-when-user-scrolls-to-the-bottom-of-a-div

@HonbraDev

HonbraDev commented Mar 29, 2020

Copy link
Copy Markdown
if ((document.querySelector("body").offsetHeight - document.documentElement.scrollTop < Math.max(document.documentElement.clientHeight, window.innerHeight || 0))) {
 // on bottom
}

better non-jquery version

@chajere

chajere commented May 13, 2020

Copy link
Copy Markdown

works perfect! I tried a lot suggestions and no one worked.. but this one is perfect!..
I'm implementing an endless scrolling function

@BerninoScuro

Copy link
Copy Markdown

potete mandare il file completo che visualizzi i valori

@BerninoScuro

Copy link
Copy Markdown

vabbè, ho trovato il modo.

@elmguz74

Copy link
Copy Markdown

Funciona al 100, thanks!

@zaqihsn

zaqihsn commented Dec 18, 2020

Copy link
Copy Markdown

find out particular elements scroll bottom
$(".user_container").scroll(function(e){
console.log()
var scrollHeight = $(this).find("table").height(); //user_container inside elements height
var currentElementHeight = $(this).height()
var scrollMax = scrollHeight - currentElementHeight;
if(scrollMax == $(this).scrollTop()){
console.log("reached at Bottom");
}
})

ghost commented Dec 5, 2021

Copy link
Copy Markdown

@areghunanyan thanks

$(window).scroll(function() {  
    var scrollHeight = window.scrollY || $(window).scrollTop();                                          
        if ((window.innerHeight + scrollHeight) >= document.body.offsetHeight) {                       
            // do something                                                    
        }                                                                                                                
}); 

I fixed little bit to work on ie too. this works fine for me.

@GabrielHilgert

GabrielHilgert commented Dec 27, 2021

Copy link
Copy Markdown

If you want to do it in a element scroll, you can do this:

// ...
        var parent = $('#parent')
        var child = $('#child')
parent.on("scroll", function() {
	if (parent.scrollTop()+parent[0].offsetHeight-child[0].scrollHeight === 0) {
	    // when scroll to bottom of the page
	}
});

@Man0j-M

Man0j-M commented Mar 10, 2022

Copy link
Copy Markdown

Awesome, thanks. This should be the first result for anyone who is searching for 'detect scroll to botton of page using jquery'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment