Skip to content

Instantly share code, notes, and snippets.

@brianmario
Created February 5, 2009 20:20
Show Gist options
  • Save brianmario/58975 to your computer and use it in GitHub Desktop.
Save brianmario/58975 to your computer and use it in GitHub Desktop.
jQuery infinite scroll plugin
// Infinite Scroll
jQuery.fn.infiniteScroll = function(options) {
return $(this).each(function() {
var el = $(this);
var settings = jQuery.extend({
url: null,
triggerAt: 300,
page: 2,
appendTo: '.list',
container: $(document)
}, options);
var req = null;
var maxReached = false;
var infinityRunner = function() {
if (settings.url !== null) {
if (settings.triggerAt >= (settings.container.height() - el.height() - el.scrollTop())) {
// if the request is in progress, exit and wait for it to finish
if (req && req.readyState < 4 && req.readyState > 0) {
return;
}
req = $.get(settings.url, 'page='+settings.page, function(data) {
if (data !== '') {
$(settings.appendTo).append(data);
settings.page++;
} else {
maxReached = true;
}
}, 'html');
}
}
};
el.scroll(function(e) {
if (!maxReached) {
infinityRunner();
}
});
// Test initial page layout for trigger
infinityRunner();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment