Ahmed Fouad
Ahmed Fouad

Reputation: 3073

How to run jQuery code when user scrolls to end of specific element?

Here is my sample code:

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(document).height() - $(window).height()) {

            // Run code here...

    }
});

I use the above code to execute stuff when user scroll to bottom of page. Instead, how can I make the code executed when user scroll to bottom of specific element (e.g. #content) and not the whole document.

Thank you.

Upvotes: 3

Views: 1361

Answers (1)

Parth Thakkar
Parth Thakkar

Reputation: 5475

This should probably work.

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(elem).position().top + $(elem).height() /* add padding if needed. */) {

            // Run code here...

    }
});

Upvotes: 5

Related Questions