1

Possible Duplicate:
Detecting when user scrolls to bottom of div with jQuery

I have a page where I have a header, left menu, content, right menu and a footer. Inside my content I have a div (say id-foo) where I have some data. When user scrolls the page and reaches the bottom of div I have to make an ajax call and bring more data to fill the content there. How can I detect that user has scrolled(page scroll, there is no scroll in div) to bottom of div (foo) .

Community
  • 1
  • 1
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146

1 Answers1

3

You'll probably have to do some math yourself, based on the current screen size and the div position. The code will look roughly like this (untested):

// Cache these values. If the div size changes, divBottom will need to be recalculated.
var $div = $("#yourdiv");
var divBottom = $div.offset().top + parseInt($div.height());

// Bind a scroll event for the whole page
$("body").bind("scroll", function(e)
{
    // Calculate how far down the user has scrolled
    var screenBottom = $(this).scrollTop() + parseInt($(window).height());

    // Test if the div has been revealed
    if(screenBottom > divBottom)
    {
        // do something
    }
});
Luke Dennis
  • 14,212
  • 17
  • 56
  • 69