-3

I am a newbie in jQuery. Basically, I need to write a query for lazy loading on a page. I found this post: Detecting when user scrolls to bottom of div with jQuery and I wanted to try the query from there:

jQuery(function($) {
  $('#pics').bind('scroll', function() {
    if($(this).scrollTop() +
       $(this).innerHeight() >= this.scrollHeight) {
      alert('end reached');
    }
  });
});

However, for me it didnt work, and I decided to create a simple (very simple) page and to test if the script will work there. Here is my code:

jQuery(function($) {
  $('#pics').bind('scroll', function() {
    if($(this).scrollTop() +
       $(this).innerHeight() >= this.scrollHeight) {
      alert('end reached');
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<h1>YEAH SANDWICHES</h1>
<div id="pics">
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
  <img src="http://placehold.it/304x228" alt="" /><br />
</div>

However, all images are pre-loaded and the jQuery script doesn't work at all. What am I doing wrong?

Community
  • 1
  • 1
sammy333
  • 1,384
  • 6
  • 21
  • 39

2 Answers2

3

Form the docs scroll():

The scroll event is sent to an element when the user scrolls to a different place in the element. It applies to window objects, but also to scrollable frames and elements with the overflow CSS property set to scroll (or auto when the element's explicit height or width is less than the height or width of its contents).

Add these css properties to #pics

Fiddle

#pics{
    overflow:scroll;  //overflow-y:scroll
    height:500px
}
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
2

You need to have a scroll on the #pics to use the scroll option. I don't know why you use jScroll, but I removed it. You can use this way:

Working Snippet

Just go to the end of the page and check the alert!

$(function() {
  $("#pics").scroll(function() {
    if($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight) {
      console.log('end reached');
    }
  });
});
#pics {height: 300px; overflow: auto;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h1>YEAH SANDWICHES</h1>
<div id="pics">
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;"/><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
  <img src="img1.png" alt="Mountain View" style="width:304px;height:228px;" /><br />
</div>
<script>
  jQuery(function($) {
    $('#pics').bind('scroll', function() {
      if($(this).scrollTop() +
         $(this).innerHeight() >= this.scrollHeight) {
        alert('end reached');
      }
    })
  });
</script>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252