Skip to content

Instantly share code, notes, and snippets.

@joeegan
Created August 24, 2011 23:09
Show Gist options
  • Select an option

  • Save joeegan/1169539 to your computer and use it in GitHub Desktop.

Select an option

Save joeegan/1169539 to your computer and use it in GitHub Desktop.
Pbm.InfiniteScroll = {};
Pbm.InfiniteScroll.count = 0;
Pbm.InfiniteScroll.init = function (url, liSelector) {
var max = $.deparam.querystring(url).max;
var endpoint = url.slice(0, url.indexOf('?'));
function displaySpinner() {
$('[data-selector=infinite-scroll-spinner]').html(Pbm.Core.loading);
}
function removeSpinner() {
$('[data-selector=infinite-scroll-spinner]').html('');
}
function displayData(data) {
if (data) {
var toShowJq = $(data).css({
opacity: 0
});
$(liSelector + ":last").after(toShowJq.animate({
opacity: 1
}, 1000));
}
}
$(window).bind('hashchange', function() {
Pbm.History.infiniteScrollCount = 0;
});
$(window).unbind('scroll');
$(window).scroll($.throttle(1000, function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
Pbm.InfiniteScroll.count++;
displaySpinner();
var postUrl = endpoint + '?start=' + ((max * Pbm.InfiniteScroll.count) + 1) + "&max=" + max;
Pbm.Service.localPost(postUrl, function (data) {
displayData(data);
Pbm.ImagePaths.populateImageSrcsFromDataUrl();
});
removeSpinner();
if (Pbm.InfiniteScroll.count > 0 && $(liSelector).length > max) {
Pbm.InfiniteScroll.displayBackToTopLink();
}
}
}));
};
Pbm.InfiniteScroll.displayBackToTopLink = function () {
if (!$('[data-selector=back-to-top]').length) {
$('body').append('<a href=# class=back-to-top data-selector=back-to-top>Back to top</a>');
var linkJq = $('[data-selector=back-to-top]');
Pbm.Urls.setCurrentUrl();
linkJq.click(function (ev) {
linkJq.remove();
ev.preventDefault();
window.scroll(0, 0);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment