Skip to content

Instantly share code, notes, and snippets.

@ilman
Created June 29, 2015 16:52
Show Gist options
  • Save ilman/75341439308601b59227 to your computer and use it in GitHub Desktop.
Save ilman/75341439308601b59227 to your computer and use it in GitHub Desktop.
jQuery Infinite Scroll
/* Load more
========================================================================== */
var loading = false;
// The number of the next page to load (/page/x/).
var pageNum = parseInt(jQuery('.load-more a').attr('data-page')) + 1;
// The maximum number of pages the current query can return.
var max = parseInt(jQuery('.load-more a').attr('data-pages'));
// The link of the next page of posts.
var nextLink = jQuery('.load-more a').attr('data-link');
/**
* Load new posts when the link is clicked.
*/
jQuery('.load-more a').click(function() {
// Are there more posts to load?
if (pageNum <= max && !loading) {
loading = true;
// Show that we're working.
jQuery(this).addClass('mim-icon-load').html('Loading posts...');
jQuery.get(nextLink, function(data) {
// Update page number and nextLink.
pageNum++;
nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/' + pageNum);
var items = Array();
var $newItems = jQuery('.element', data);
//$newItems.imagesLoaded(function(){
jQuery('#isotope').append( $newItems ).isotope( 'addItems', $newItems, function () {
jQuery(window).resize();
$newItems.each(function() {
if (jQuery(this).hasClass('isotope-item')) jQuery(this).removeClass('isotope-item').addClass('isotope-item.no-transition');
if (jQuery(this).css('opacity') == '0') items.push(jQuery(this));
});
setTimeout(function() {
loading = false;
jQuery('.load-more a').removeClass('mim-icon-load').html('More Items...');
twttr.widgets.load();
loadFlexSlider();
loadItems(items);
//jQuery('.entry-thumb:not(.slide)').overThumb();
jQuery('#isotope').isotope('reloadItems');
jQuery(window).resize();
if (pageNum > max) jQuery('.load-more').remove();
}, 100);
} );
//});
});
} else {
}
return false;
});
/* Infinite scroll
========================================================================== */
if (jQuery('body.infinite-scroll').length) {
var count = 2;
jQuery(window).scroll(function(){
if (jQuery(window).scrollTop() == jQuery(document).height() - jQuery(window).height()){
loadArticle(count);
count++;
}
});
function loadArticle(pageNumber){
jQuery('.load-more a').trigger('click');
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment