I've looked through various questions on SO regarding this question, but none seems to solve my problem.
When the page first loads, I initiated masonry on the first ten images as follows:
$('img').load(function(){
var masonry = $('#gallery').masonry({
itemSelector: '.item',
});
});
Next, when a user scrolls to the bottom of a page, I call an ajax:
if ($(window).scrollTop() >= $(document).height() - $(window).height() + 5) {
request_pending = true;
$('#loading').show();
$.ajax({
url: '/timeline/getPhotosAPI',
type: 'POST',
data: { id:'<?=$id?>', offset: offset },
success: function(html){
if(html.length > 0){
var response = JSON.parse(html);
$.each(response, function(i, item){
$('#gallery').append(item);
var $html = $(item);
$('#gallery').find('.item').masonry('appended', $html, true);
});
$('#loading').hide();
offset = parseInt(offset) + 10;
request_pending = false;
}else{
alert('fail');
}
}
});
}
Where response is a JSON string of html DOMS from my php that looks something like :
array( 0 => '<div class="items"><img src="xyz" /></div>" );
The above code successfully appends my images into my #gallery
container, but masonry doesnt read my ".item" classes and append the respective positioning to my newly appended items.
Any ideas?