Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap isotope and lazyload fetch/load images via json

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');


    });
    //});
});
like image 950
django Avatar asked Nov 03 '22 19:11

django


1 Answers

I solved it.Working Fiddle

Issues that were causing failure:

  1. 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">

  2. 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.

like image 183
django Avatar answered Dec 28 '22 11:12

django