What I'm trying to do is push new ".post" (which have been retrieved by the infinite-scroll script) into my array which holds all of the ".post".
Full Code: https://gist.github.com/sxxfi/8296403
Live Demo: http://soof-show.tumblr.com (navigate through posts with keyboard arrow keys)
Initially, five ".post" show up in the ".content" div, then each time the 'MORE' button is clicked it adds 5 more ".post" to ".content".
The problem is the array length doesn't count for the new ".post" so it keeps looping the first 5.
Thank you in advance if you're able to help solve this.
Javascript:
$(document).ready(function(){
// I N F I N I T E S C R O L L
$('.content').infinitescroll({
navSelector : ".navigation",
nextSelector : ".navigation .next_page",
itemSelector : ".content .post",
behavior: "twitter"
});
$(window).unbind('.infscr');
$('.next_page').click(function(){
$(document).trigger('retrieve.infscr');
return false;
});
$(document).ajaxError(function(e,xhr,opt){
if (xhr.status == 404) $('.next_page').remove();
});
// I M A G E S L I D E R
var obj = $(".post");
var arr = $.makeArray(obj);
var lastDiv = $(".post");
var indexNum = arr.length-1;
$(window).keydown(function (e) {
if (e.keyCode == 37) { // L E F T
lastDiv.fadeOut(800);
indexNum--;
if (indexNum == -1) indexNum = arr.length-1; //if goes beyond first post, goto the last post
$(arr[indexNum]).appendTo(".postslide").fadeIn(800);
// C E N T E R
var width = $(arr[indexNum]).width();
var marginLeft = width / 2;
$(arr[indexNum]).css('margin-left', -marginLeft);
}
if (e.keyCode == 39) { // R I G H T
$('#instruction').fadeOut();
lastDiv.fadeOut(800);
indexNum++;
if (indexNum == arr.length) indexNum = 0; //if goes beyond last post, goto the first post
$(arr[indexNum]).appendTo(".postslide").fadeIn(800);
// C E N T E R
var width = $(arr[indexNum]).width();
var marginLeft = width / 2;
$(arr[indexNum]).css('margin-left', -marginLeft);
}
});
});
UPDATE:
$(document).ready(function(){
// I N F I N I T E S C R O L L
$('.content').infinitescroll({
navSelector : ".navigation",
// selector for the paged navigation (it will be hidden)
nextSelector : ".navigation .next_page",
// selector for the NEXT link (to page 2)
itemSelector : ".content .post",
// selector for all items you'll retrieve
behavior: "twitter"
});
// kill scroll binding
$(window).unbind('.infscr');
// manual click
$('.next_page').click(function(){
$(document).trigger('retrieve.infscr');
return false;
});
// remove the paginator done
$(document).ajaxError(function(e,xhr,opt){
if (xhr.status == 404) $('.next_page').remove();
});
// I M A G E S L I D E R
var obj = $(".post");
var indexNum = obj.length-1;
var lastDiv = $(".post");
$(window).keydown(function (e) {
if (e.keyCode == 37) { // L E F T
lastDiv.fadeOut(800);
indexNum--;
if (indexNum == -1) indexNum = obj.length-1; //if goes beyond first post, goto the last post
$(obj[indexNum]).appendTo(".postslide").fadeIn(800);
// C E N T E R
var width = $(obj[indexNum]).width();
var marginLeft = width / 2;
$(obj[indexNum]).css('margin-left', -marginLeft);
}
if (e.keyCode == 39) { // R I G H T
$('#instruction').fadeOut();
lastDiv.fadeOut(800);
indexNum++;
if (indexNum == obj.length) indexNum = 0; //if goes beyond last post, goto the first post
$(obj[indexNum]).appendTo(".postslide").fadeIn(800);
// C E N T E R
var width = $(obj[indexNum]).width();
var marginLeft = width / 2;
$(obj[indexNum]).css('margin-left', -marginLeft);
}
});
});