Udemy

How to detect user reached bottom of page while scrolling

Sunday, November 03, 2013 0 Comments A+ a-

Few days before i needed to implement on scroll paging as the facebook or linkedin is doing, instead of paging i thought to do infinite page scroll paging in my application so i decided to share the thing here as well.


Here is a sample code. Add this html to a empty html page:


<div style="height: 4000px">Scroll down!</div>

Here is the jquery code, but remeber to add jquery minified version in your page head as well like this:


<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>


Here is the jquery code to detect if user has reached page end while scrolling:


<script type="text/javascript">

        $(window).scroll(function () {
            if ($(window).scrollTop() + $(window).height() == $(document).height()) {
                alert("bottom!");
            }
        });

    
    </script>


here is the code to detect if user is near the bottom:


 $(window).scroll(function() {   
   if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
       alert("bottom!");
   }
});

Hope you got it. -:)

Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more