2

I am loading images into a div using .append()

How can I use the lazy loader plugin on these newly appended images? Is there a way to do it?

UPDATE: Here's the code

//This is a live search script, type something in #livesearch input and it populates a #results ul list with the results
$('#livesearch').live('keyup', function(event) {
    //after ke press detected immediately clear the results div of any previous content
    $('#results').html("");
    // Use the inputed letters to send off an ajax request
    $.getJSON("http://xxxxxx.com/live/" + $(this).val(), function(resp){
            //the ajax returns a json object, next loop through the object and print out the results in a list using append
            $.each(resp.response.docs, function(index, item) {  
            $('#results').append("<li><div style='background-color:rgb(31,31,31);padding:15px;color:rgb(255,255,255);'><span class='inline' style='width:125px;height:50px;margin-right:15px'><img data-original='img/"+item.nameid+".png' src='img/grey.gif' class='lazy' style='display:inline' height='50px'></span><span class='inline' style='width:300px;'><div style='font-size:14px;'>"+item.name+"</div><div style='font-size:12px;color:rgb(99,99,99)'>Strategy | 4 months ago | Played 2,000 times</div></span></div></li>");
        });
    });

    // The list that was created includes images and could have several hundred of them, therefore I would like to lazy load them as you scroll down
    // This line of code is just one of my failed attempts
    $("img.lazy").lazyload();
});

Because the images are added after the document has loaded I think the lazy loader plugin has difficulty 'seeing' the images in the original HTML since they do not exist there. Perhaps LazyLoad is not capable of doing what I want it to.

4
  • code added, I've commented it so it is easier to understand Commented Dec 4, 2012 at 23:05
  • Do you have a link to a demo? Commented Dec 5, 2012 at 22:02
  • api.jquery.com/live Commented Dec 7, 2012 at 11:16
  • I have the same problem. I use lazyLoad after .append(). Every functions I put in $(document).ready(). Any solution? Commented May 27, 2013 at 7:19

4 Answers 4

1

Call lazyload() from your getJSON callback

$.getJSON("http://xxxxxx.com/live/" + $(this).val(), function(resp){
    $.each(resp.response.docs, function(index, item) {  
        $('#results').append("<li>... \
            <img data-original='img/"+item.nameid+".png' \
            src='img/grey.gif' class='lazy' /> \
            ...</li>");
        });
    });
    $("img.lazy").lazyload().removeClass("lazy");
});

Adding .removeClass("lazy") prevents lazyload from binding to the same element multiple times.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Enrico, thanks, but no luck. Just as before my images simply do not load at all.
1

I use these 2 alternatives

1) Modify lazyload.js

Search this line

$(window).load(function() {
     update();
});

and replace with this

$(function () {
     update();
});

this is for both if page is loaded for first time or if by ajax call

for add lazyload to new elements write:

$( newElements ).find("img.lazy").lazyload();

if the new images aren't loaded yet, you have to prevent that the images be rendered in layout (because they do not have dimensions) Lazy Load might load them all (because they all are in viewport) so use this:

setTimeout(function(){
 $( newElements ).find("img.lazy").lazyload();
} ,500);

2) Or you can use this to force to show the images

$("img.lazy").lazyload().trigger("appear");

Comments

1

jQuery now defines a when() function for the purpose of doing something after jQuery Ajax request(s) is/are done

// the following code will be executed when the ajax request resolve.
// the ajax function must return the value from calling the $.ajax() method.

$.when(ajax1()).done(function(a1){

        if($('img.lazy').length){

             $('img.lazy').lazyload({ 

                      effect:'fadeIn', 
                      threshold:500, 
                      failure_limit:2 

             }).removeClass('lazy').addClass('lazyloaded');

        }

    });

    function ajax1() {

        return $.ajax({
            url: "someUrl",
            dataType: "json",
            data:  yourJsonData,            
            ...
        });
    }

from: http://api.jquery.com/jQuery.when/

Comments

0

I knew how to solve this problem. You should put $("img.lazy").lazyload(); after you have done every dynamic appending.

In your situation, you must put it in function(index, item) (after append line).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.