Alert using Jquery when Scroll to end of Page

后端 未结 6 1758
余生分开走
余生分开走 2020-12-31 15:43

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.

6条回答
  •  一向
    一向 (楼主)
    2020-12-31 16:07

    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

提交回复
热议问题