Skip to content

Instantly share code, notes, and snippets.

@rtt
Created March 13, 2013 16:44
Show Gist options
  • Select an option

  • Save rtt/5153956 to your computer and use it in GitHub Desktop.

Select an option

Save rtt/5153956 to your computer and use it in GitHub Desktop.
var infinite_scroll_config = {
binder: $(window),
el: '.infinite-scroll',
offset: 750,
in_req: false,
at_end: false
};
var infinite_scroll = function () {
if ($(infinite_scroll_config.el).length === 0) {
console.log('infinte scroll not active');
return;
}
$(window).on('scroll', function (e) {
// get last of the .infinite-scroll markers
if (infinite_scroll_config.at_end) {
console.log('end of infinite scroll reached');
$(window).off('scroll');
return;
}
var markers = $(infinite_scroll_config.el);
// get the 'last' marker, which in theory is the one furthest down the page
var last = markers.slice(-1);
if (!infinite_scroll_config.in_req && ((last.offset().top - infinite_scroll_config.binder.scrollTop()) < infinite_scroll_config.offset)) {
infinite_scroll_config.in_req = true;
console.log('item {0} came into view'.format($(last).data('id')));
$.ajax({
type: 'GET',
url: $.C.LocalePath('/xhr/newsfeed/{0}/'.format($(last).data('id'))),
dataType: 'json',
success: function (d, status, jqxhr) {
if (d.at_end) {
infinite_scroll_config.at_end = true;
}
$('.stream').append(d.markup);
// take us out of in_request mode
// a fake async, if you will
infinite_scroll_config.in_req = false;
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment