我正在通过 JSON 将图像加载到#photographs 中。它最初加载 10 个镜头,当您向下滚动到某个点(使用航路点)时,它应该“刷新”JSON 提要,以便加载额外的 5 个镜头。我在 JSON URL 中使用 // &per_page=' + itemsLoaded // 为此。每次击中航路点时,var itemsLoaded 都会更新为 +5。
见代码:
var itemsLoaded = 10;
$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=' + itemsLoaded + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function (data) {
$.each(data.photoset.photo, function (i, flickrPhoto) {
var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/' + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_b.jpg";
var basePhotoURLMobile = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/' + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_z.jpg";
var flickrLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";
var $img = $("<img/>").attr("src", basePhotoURL);
var $imgMobile = $("<img/>").attr("src", basePhotoURLMobile);
var $wrap = $("<div class='item'></div>");
$(".item:nth-child(9n)").addClass("tenth");
if($(window).width() < 501) {
$wrap.append($imgMobile);
} else {
$wrap.append($img);
}
$wrap.append("<a href='" + basePhotoURL + "'.jpg' title='View full size' class='zoom' rel='enroll' />");
$wrap.append("<a href='" + flickrLink + "' class='flickr' title='View on Flickr' target='_blank' />");
$wrap.appendTo('#photographs');
});
var loaded = 0;
var totalAmount = $('#photographs .item').length;
$('#photographs .item img').each(function() {
loaded;
$(this).imagesLoaded(function($images) {
loaded++;
var percentage = parseInt((loaded / 11) * 100);
console.log(loaded + ' van de ' + totalAmount);
$("#bigloader").progressbar({
value: percentage
});
if(loaded == 10) {
$("#photographs, #loader").fadeIn("fast");
$("#bigloader, #preloading").fadeOut("fast");
$("#photographs").gridalicious({
gutter: 2,
animate: true,
effect: 'fadeInOnAppear',
width: 430
});
$('.item.tenth').waypoint({
triggerOnce: true,
handler: function(){
console.log("arrived at trigger");
itemsLoaded = itemsLoaded + 5;
},
offset: '50%'
});
} else if (loaded == totalAmount) {
$("#loader").fadeOut("fast");
};
});
});
额外说明:
每次击中航点时,itemsLoaded 变量都会更新为 +5 值。itemsLoaded 是我通过 JSON 加载的项目数量的关键;以便 JSON 提要需要刷新。
基本上,我如何使用更新的变量更新现有的 JSON 提要?