views:

399

answers:

2

I have a bunch of images that are in a hidden div. The div is shown when the user clicks on some links.

I'd like to jQuery lazyload to hide these images until the link is clicked and the hidden div is exposed. But, if I use lazyload according to the documentation, the images are always loaded as the hidden div is in the viewport, presumably.

any ideas?

+1  A: 

Do you really need the lazy load plugin? How about replacing the div content with the image(s) when you click the link?

Something like:

<a href="link" onclick="show_images('div_id');">asdf</a>

and

function show_images(id) { 
    $('#' + id).append('<img src="image1.jpg" /><img src="imagex.jpg" />'); 
}
Timo
Should be a comment.
Josh K
Thing is... I don't have 50 points to leave a comment. Also the second question in question is an alternative solution to the problem which I detailed a bit more in my edit.
Timo
+1 Timo. This is a simple solution and shouldn't be a comment as it answers the question. This works in Google Chrome, but I don't know if it would dynamically load images like this in other browsers.
Jim Schubert
Now it answers the question. Previously it was a comment. If you don't have the rep to leave a comment then don't provide a comment under the guise of an answer.
Josh K
@Josh K: I see what you mean. It's pretty tough posting comments as answers until 50 rep.
Jim Schubert
I know beforehand that I shouldn't keep this up, but I would say the two unedited questions do answer the "any ideas?" question (albeit with too little detail). Anyway I'm new here so fire away ;) My apologies for any inconvenience.
Timo
You adjusted the answer, it's fine.
Josh K
A: 

Timo I think he doesn't want anyway that loads images in the dom before he needs them. Somehow this is a classic scenario of using ajax to load content.

I propose that you go about it like this

$.fn.image = function(src, f) {

        return this.each(function() {

            var i = new Image();

            i.src = src;

            i.onload = f;

            this.appendChild(i);

        });
    }

    $(function() {


        $('yourlink').click(function() {

            $("#container").image("image.jpg", function() {

                alert("image loaded");

            });
        });

    });
XGreen
or you can have the images in another html and loads them via ajax function when you need them
XGreen