Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert using Jquery when Scroll to end of Page

Is there a way to find out page end using Jquery, so that a simple message can be displayed saying you have reached end of the page.

like image 574
Sandhurst Avatar asked Sep 26 '10 19:09

Sandhurst


4 Answers

How to tell when you're at the bottom of a page:

if (  document.documentElement.clientHeight + 
      $(document).scrollTop() >= document.body.offsetHeight )
{ 
    // Display alert or whatever you want to do when you're 
    //   at the bottom of the page. 
    alert("You're at the bottom of the page.");
}

Of course you want to fire the above whenever the user scrolls:

$(window).scroll(function() {
    if (  document.documentElement.clientHeight + 
          $(document).scrollTop() >= document.body.offsetHeight )
    { 
        // Display alert or whatever you want to do when you're 
        //   at the bottom of the page. 
        alert("You're at the bottom of the page.");
    }
});

Here is a jsFiddle example that fades in a "You're Done! Scroll to Top of Page" link when the user has scrolled to the bottom of the page.

References:

  • .scroll()
  • .scrollTop()
  • offsetHeight
  • clientHeight
like image 184
Peter Ajtai Avatar answered Oct 22 '22 08:10

Peter Ajtai


This will work and I tested it in IE 7,8,9 , FF 3.6, Chrome 6 and Opera 10.6

$(window).scroll(function()
{
    if (document.body.scrollHeight - $(this).scrollTop()  <= $(this).height())
    {
        alert('end');
    }
});
like image 28
Matthew Manela Avatar answered Oct 22 '22 09:10

Matthew Manela


If the above solutions don't work please check if you set your document type right:

<!DOCTYPE HTML>

Took me an hour to find out :)

like image 21
Denny Beulen Avatar answered Oct 22 '22 10:10

Denny Beulen


To avoid duplicate console.log('end of page'), you need create a setTimeout, like this:

var doc = $(document), w = $(window), timer;

doc.on('scroll', function(){

    if(doc.scrollTop() + w.height() >= doc.height()){

        if(typeof timer !== 'undefined') clearTimeout(timer);

        timer = setTimeout(function(){
            console.log('end of page');
        }, 50);

    }

});
like image 43
Gerson Lima Avatar answered Oct 22 '22 10:10

Gerson Lima