I've spent considerable amount of time trying to get isotope and lazy loading working together.
The issue: lazy loading works if the user scrolls down, however if the user uses the filters, the items show up on top but the images will not load.
Here is someone with the same issue, but it seems he fixed it. I tried several things but still couldnt get it working.
Here is the dicussion https://github.com/tuupola/jquery_lazyload/issues/51
Thanks alot for your help
The code I am using is as follow.
jQuery(document).ready(function($) {
$('#big_container .media_block img').each(function(index) {
var item_height = $(this).attr("height");
$(this).parent().parent().css("height",item_height);
});
$('#big_container').isotope({
itemSelector : '.item',
layoutMode : 'masonry',
masonry: {
columnWidth: 5,
},
sortBy : 'date',
sortAscending : false,
getSortData : {
date : function ( $elem ) {
return $elem.find('.date').text(); // Date format should be [Y-m-d H:i]
},
views : function( $elem ) {
return parseInt( $elem.attr('data-views'), 10 );
},
//featured : function ( $elem ) {
// return $elem.attr('data-featured');
// },
rates : function( $elem ) {
return parseInt( $elem.attr('data-rates'), 10 );
},
comments : function( $elem ) {
return parseInt( $elem.attr('data-comments'), 10 );
}
}
});
$('#sort-by li a').click(function(){
var $this = $(this);
if ($(this).parent().hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents();
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
var sortName = $(this).attr('href').slice(1);
$('#big_container').isotope({ sortBy : sortName });
return false;
});
});
To get isotope's sorting/filtering to work with lazyload you have to do the following.
jQuery(document).ready(function($) {
var $win = $(window),
$con = $('#container'),
$imgs = $("img.lazy");
$con.isotope();
$con.on('layoutComplete', function(){
$win.trigger("scroll");
});
$imgs.lazyload({
failure_limit: Math.max($imgs.length - 1, 0)
});
});
According to the docs ( http://www.appelsiini.net/projects/lazyload )
After scrolling page Lazy Load loops though unloaded images. In loop it checks if image has become visible. By default loop is stopped when first image below the fold (not visible) is found. This is based on following assumption. Order of images on page is same as order of images in HTML code. With some layouts assumption this might be wrong.
With an isotope sorted/filtered list, the page order is certainly different from the HTML so we need to adjust our failure_limit
.
As you can see we store the jQuery object so that we can use its length-1
as our failure_limit. If you're curious as to why it is length-1
, it's because of the following check in lazyload's update method.
if (++counter > settings.failure_limit) {
return false;
}
If you are not triggering your lazyloads on scroll, you will need to swap the "scroll" trigger for whichever event you are using.
http://jsfiddle.net/arthurc/ZnEhn/
I stored some of the jQuery objects in variables as they need to be re-used.
jQuery(document).ready(function($) {
var $window = $(window);
var $images = $('#big_container .media_block img');
var $big_container = $('#big_container');
$images.each(function(index) {
var item_height = $(this).attr("height");
$(this).parent().parent().css("height",item_height);
});
$big_container.isotope({
itemSelector : '.item',
layoutMode : 'masonry',
masonry: {
columnWidth: 5,
},
sortBy : 'date',
sortAscending : false,
getSortData : {
date : function ( $elem ) {
return $elem.find('.date').text(); // Date format should be [Y-m-d H:i]
},
views : function( $elem ) {
return parseInt( $elem.attr('data-views'), 10 );
},
//featured : function ( $elem ) {
// return $elem.attr('data-featured');
// },
rates : function( $elem ) {
return parseInt( $elem.attr('data-rates'), 10 );
},
comments : function( $elem ) {
return parseInt( $elem.attr('data-comments'), 10 );
}
}
});
$big_container.on('layoutComplete', function(){
$win.trigger("scroll");
});
$('#sort-by li a').click(function(){
var $this = $(this);
if ($(this).parent().hasClass('selected') ) {
return false;
}
var $optionSet = $this.parents();
$optionSet.find('.selected').removeClass('selected');
$this.addClass('selected');
var sortName = $(this).attr('href').slice(1);
$big_container.isotope({ sortBy : sortName });
return false;
});
$images.lazyload({
failure_limit : Math.max($images.length-1, 0);
})
});