Michael
Michael

Reputation: 863

Jquery preload image dynamically

Wondering why this is not working...

Function:

$.fn.preload = function() {
        this.each(function(){
            $('<img/>')[0].src = this;
        });
    }

Code:

<?php $preimages = glob('images/backgrounds/*{.jpg, .png, .gif, jpeg}', GLOB_BRACE); ?>

        $([
            <?php 
                $imgcount = sizeof($preimages);
                for($i = 0; $i < $imgcount; $i++) {
                    if($i == $imgcount-1)
                        echo $preimages[$i];
                    else
                        echo $preimages[$i] . ', ';
                }

            ?>
            ]).preload($("#load").fadeOut());

I'm trying to preload all images in the foler images/backgrounds folder... but then i want the callback to be $("#load").fadeOut()...

However it works when I do this like following:

img1 = new Image();
        img2 = new Image();
        img3 = new Image();

        img1loaded = false;
        img2loaded = false;
        img3loaded = false;

        img1.onload = function() {
            img1loaded = true;
            if(img1loaded && img2loaded && img3loaded) {
                $("#load").fadeOut();
            }
        };

        img2.onload = function() {
            img2loaded = true;
            if(img1loaded && img2loaded && img3loaded) {
                $("#load").fadeOut();
            }
        };

        img3.onload = function() {
            img3loaded = true;
            if(img1loaded && img2loaded && img3loaded) {
                $("#load").fadeOut();
            }
        };

        img1.src = '<?php echo $baseUrl; ?>images/backgrounds/img1.jpg';
        img2.src = '<?php echo $baseUrl; ?>images/backgrounds/img2.jpg';
        img3.src = '<?php echo $baseUrl; ?>images/backgrounds/img3.jpg';

How can I do what i do above, but dynamically?

Thanks!

Upvotes: 2

Views: 1100

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

Here's an image preloader I've used before...

function onImagesLoaded($container, callback) {
    var $images = $container.find("img");
    var imgCount = $images.length;
    if (!imgCount) {
        callback();
    } else {
        $("img", $container).each(function () {
            $(this).one("load error", function () {
                imgCount--;
                if (imgCount == 0) {
                    callback();
                }
            });
            if (this.complete) $(this).load();
        });
    }
}

Call it by passing it a parent element (as a jQuery object, not a DOM element), and a callback function, for when all the images are loaded...

onImagesLoaded($("div.image-container"), imagesLoaded);

That will call imagesLoaded() when all the images in div.image-container are loaded.

It could be expanded upon to handle errors, but for my purposes I just treat an error as the image is complete, otherwise it wouldn't run the callback function if any of the images errored for any reason.

Upvotes: 2

Related Questions