I'm writing a little jQuery script for my tumblr page, that emulates infinite scroll loading via jQuery.get()
the content that I want from the next pages of the site (it's a tumblr site, so the pagination is OK).
I know that there are some infinite scroll plugins, but all I tried not working for me and, besides, are pretty fatty. I don't need all that funcionality.
The script loads the new elements that I want, yes, but the problem is that loads the content from two pages every time. And, obviously, it should load instead only a single page content.
The jQuery is:
$(document).ready(function() {
"use strict";
var nextPage = 2;
var totalPages = {TotalPages};
var url = '/page/'; // base url
$(document).on('scroll', function() {
if (nextPage <= totalPages) { // if exist new pages
if ($(window).scrollTop()== $(document).height() - $(window).height()) { // when reach the page bottom
$.get(url + nextPage, function(data){ // get the next page
$(data).find(".post").appendTo(".posts"); // filter content we want and append it to the actual page element
picturefill(); // reload picturefill,making the new images responsive
});
nextPage++; // increment nextpage counter
};
} else { // exit if no more pages
return;
}
});
});
And the content that it loads is:
<div class="posts">
<article class="photo-container post" id="{PostID}" data-reblog="{ReblogURL}">
<div class=" picturefill" data-picture data-alt="" data-class="photo">
<div class="picturefill" data-src="{PhotoURL-HighRes}" ></div>
<div class="picturefill" data-src="{PhotoURL-500}" data-media="(max-width: 568px)"></div>
<div class="picturefill" data-src="{PhotoURL-250}" data-media="(max-width: 250px)"></div>
<img alt="" class="photo" src="http://img.jpg"> <!-- generated by picturefill -->
<noscript><img class="photo" src="{PhotoURL-HighRes}" alt=""></noscript>
</div>
<!-- MORE CODE -->
</article>
I use Picturefill for responsive images and that works well. Anybody have a idea? Thanks.