我正在为我的 tumblr 页面编写一个小jQueryjQuery.get()
脚本,该脚本通过我想要从站点的下一页(它是一个tumblr站点,所以分页可以)的内容来模拟无限滚动加载。
我知道有一些无限滚动插件,但我尝试过的所有插件都不适合我,而且非常胖。我不需要所有的功能。该脚本会加载我想要的新元素,是的,但问题是每次都从两个页面加载内容。而且,显然,它应该只加载单个页面内容。
jQuery是 :
$(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;
}
});
});
它加载的内容是:
<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>
我将Picturefill用于响应式图像,效果很好。有人有想法吗?谢谢。