Basit Basit - 17 days ago 6
Javascript Question

jQuery load more data on scroll

im just wondering how can i implement more data on scroll only if the div.loading is visible.

usually we look for page height and scroll height, to see if we need to load more data. but the following example is little complicated then that.

following image is perfect example. there are two .loading div's on the drop down box. when user scroll the content, whichever is visible it should start loading more data for it.

enter image description here

so how can i find out if .loading div is visible to user yet or not? so i can start loading data for that div only.

Answer

Have you heard about the jQuery Waypoint plugin.

Below is the simple way of calling a waypoints plugin and having the page load more Content once you reaches the bottom on scroll :

$(document).ready(function() {
    var $loading = $("<div class='loading'><p>Loading more items&hellip;</p></div>"),
    $footer = $('footer'),
    opts = {
        offset: '100%'
    };

    $footer.waypoint(function(event, direction) {
        $footer.waypoint('remove');
        $('body').append($loading);
        $.get($('.more a').attr('href'), function(data) {
            var $data = $(data);
            $('#container').append($data.find('.article'));
            $loading.detach();
            $('.more').replaceWith($data.find('.more'));
            $footer.waypoint(opts);
        });
    }, opts);
});