Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
});
@Choko256

This comment has been minimized.

Copy link

commented Jul 31, 2015

Works perfectly 👍 👍

@Irostovsky

This comment has been minimized.

Copy link

commented Sep 22, 2015

Excelent!

@ysksn

This comment has been minimized.

Copy link

commented Oct 6, 2015

like it :)

@hoanghiep90

This comment has been minimized.

Copy link

commented Oct 20, 2015

Work like a boss

@jamshidasghari

This comment has been minimized.

Copy link

commented Jan 7, 2016

thanks but this command don't work for me.
$(window).height() is equal to $(document).height() ?

@amigoow

This comment has been minimized.

Copy link

commented Jan 18, 2016

Works inversely i.e when I scroll to the top of page only then it fires. Probably my system scroll is inverse?

@ultrasamad

This comment has been minimized.

Copy link

commented Jan 19, 2016

@amigoow, I also had the same issue. I realized that my html, head and title elements were in caps. After changing them to lower cases, everything work perfect.

@Shaz3e

This comment has been minimized.

Copy link

commented Mar 22, 2016

This works too

    $(window).scroll(function(){
        if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
            $('.s3cc-fixed').css('position','static');
        }else{
            $('.s3cc-fixed').css('position','fixed');
        }
    });
@chrisjrturing

This comment has been minimized.

Copy link

commented May 3, 2016

great commit !! thx

@ghost

This comment has been minimized.

Copy link

commented Jan 25, 2017

awesome!

@jimfilippou

This comment has been minimized.

Copy link

commented May 7, 2017

Works! so happy! 😄

@liuliangsir

This comment has been minimized.

Copy link

commented May 27, 2017

awesome

@areghunanyan

This comment has been minimized.

Copy link

commented May 28, 2017

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
            }
        });
@pohka

This comment has been minimized.

Copy link

commented Jul 22, 2017

@areghunanyan it worked amazingly, thanks

@VictoriaLau

This comment has been minimized.

Copy link

commented Aug 15, 2017

awesome!

@this-is-karthikking

This comment has been minimized.

Copy link

commented Nov 24, 2017

@areghunanyan Pukka... Thank you so much.....

@vishalkumarchaudhary

This comment has been minimized.

Copy link

commented Dec 14, 2017

Thanks a lot .
can you show how you can detect end of any html tag which has some class

@leonidaswander

This comment has been minimized.

Copy link

commented Jan 24, 2018

@areghunanyan thanks

@skptricks

This comment has been minimized.

@caceres97

This comment has been minimized.

Copy link

commented Feb 24, 2018

Great idea!!

@corics15

This comment has been minimized.

Copy link

commented Apr 6, 2018

Nice!

@chandrakailash1993

This comment has been minimized.

Copy link

commented Apr 13, 2018

not working in tablet

@nfouka

This comment has been minimized.

Copy link

commented Jul 26, 2018

Good ! Tanks @

@kehollin

This comment has been minimized.

Copy link

commented Aug 8, 2018

I like your used of explicit variables for clarity, but the difference between your scrollHeight and scrollPosition never went to zero because the $(window).scrolltop was a float/double that never maxed out, at least in Chrome Version 68.0.3440.84 (Official Build) (64-bit). Here's my solution:

    $(window).on("scroll", function() {
        var docHeight = $(document).height();
        var winScrolled = $(window).height() + $(window).scrollTop(); // Sum never quite reaches
        if ((docHeight - winScrolled) < 1) {
            console.log("event " + "module scrolled to bottom" + " @Model.documentFilename @Model.requestorId");
        }
    });
@alienjedi

This comment has been minimized.

Copy link

commented Aug 14, 2018

All of these damn solutions do not work on mobile devices

@tiger8888

This comment has been minimized.

Copy link

commented Aug 28, 2018

thanks! my boss

@phuonglop12a2

This comment has been minimized.

Copy link

commented Aug 31, 2018

Thanks! Work Good!

@OneAdder

This comment has been minimized.

Copy link

commented Sep 1, 2018

Thanks a lot!

@mamster12

This comment has been minimized.

Copy link

commented Nov 21, 2018

works perfectly on desktop and mobile version!

@doganoo

This comment has been minimized.

Copy link

commented Nov 24, 2018

works perfect, thank you!

@Thierkaz

This comment has been minimized.

Copy link

commented Dec 2, 2018

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

This comment has been minimized.

Copy link

commented Dec 20, 2018

awesome

@enetarch

This comment has been minimized.

Copy link

commented Jan 11, 2019

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

This comment has been minimized.

Copy link

commented Feb 1, 2019

Does this work in mobile browser with responsive layout bootstrap?

@Sathya-Kasithangam

This comment has been minimized.

Copy link

commented Mar 21, 2019

Is it possible to pause between scrolls?

@icryptix

This comment has been minimized.

Copy link

commented Mar 26, 2019

Does Not work on mobile

@maidulcu

This comment has been minimized.

Copy link

commented Jun 15, 2019

Will this work on mobile on tap?

@Asad2175

This comment has been minimized.

Copy link

commented Jul 1, 2019

but it does not works on the popup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.