Maven Maven - 6 months ago 43
Javascript Question

How to detect scroll position of page using jQuery

I am having following jQuery functionality in my website, what it does that is it uses

window.scroll()
function to recognize when the windows changes scroll position changes and at the change call few function to load data from server.

The problem is the
.scroll()
function is called as soon as there is even a little change in the scroll position and load data at the bottom however what I wish to achieve is load new data when the scroll/page position reaches at the bottom, like is happens for Facebook feed.

But I am not sure how to detect scroll position using jQuery?

function getData() {
$.getJSON('Get/GetData?no=1', function (responseText) {
//Load some data from the server
})
};

$(window).scroll(function () {
getData();
});

Answer

You can extract the scroll position using jQuery's .scrollTop() method

$(window).scroll(function (event) {
    var scroll = $(window).scrollTop();
    // Do something
});