Normal isotope usage for ajax fetching is working. see working jsfiddle.
isotope with lazyload via ajax fetch not working. see problem jsfiddle.
Problem: lazyload does not trigger and keeps showing grey image.
javaScript for lazyload setup :
$(document).ready(function () {
//
// initialize at ready ;
//
$container.isotope({
itemSelector: '.box',
columnWidth: function (containerWidth) {
return containerWidth / 12;
},
onLayout: function () {
$win.trigger("scroll");
}
});
//
// here i will be using data through api
// For now I am defining json manually
// var json is defined at top of this code
// considering json return was success
//$.getJSON(APIURL, function (json) {
var newElements = "";
$.each(json, function (key, value) {
console.log(key);
newElements +=
'<div class="box">' +
'<img class="lazy" src="' + small_img + '" data-originalsrc="' + value['image'] + '" width="' + value['width'] + '" height="' + value['height'] + '" />' +
'</div>';
});
var $newElems = $(newElements);
$container.append($newElems).imagesLoaded(function () {
$container.isotope('appended', $newElems);
$imgs.lazyload({
container: $container,
effect: "fadeIn",
}).removeClass("lazy");
$imgs.trigger('scroll');
});
//});
});
I solved it.Working Fiddle
Issues that were causing failure:
Bootstrap I had to put style="width:XX;height:XX" in image tag too along with normal width and height parameter to overwrite bootstrap img{} css definitions which were one of problems. <img class="lazy" width="200" height="300" style="width: 200px; height: 300px;" data-original="abc.jpg" src="lazygrey.gif">
vars Before AJAX: Since I am using ajax content so var defined like var $imgs= $("img.lazy");
before ajax call doesnot work. So to be safe I used $("img.lazy") directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With