Lucas Veiga
Lucas Veiga

Reputation: 1795

Load Image and Fade In, but in sequential way

I'm trying to load my images and fade in. Its working. But lets suppose that I have 4 images. Right now, my script its working by loading the last image, not the first. How can I make my js load the first image, and just start loading the second after the first has been loaded?

Here's my code:

$(function(){
    $("a").click(function(){
        $("#load").load('load.html', function() {
            $('#load img').hide();
            alert($("#load img").length);

            $('#load img').each( function(){
                $(this).on('load', function () {
                    $(this).fadeIn();
                });
            });
        });
        return false;
    });
});

Upvotes: 3

Views: 557

Answers (2)

Pierre
Pierre

Reputation: 1227

Ron's solution is a little over-engineered IMO. If your intention is indeed to load all of the images before the animation starts, then you could do something similar to the following:

$(function(){
    $("a").click(function(){
        $("#load").load('load.html', function() {
            $('#load img').hide(); // I would recommend using css to hide the image instead
            alert($("#load img").length);

            $("#load img").each(function(i, image) {
               setTimeout(function() { // For each image, set increasingly large timeout before fadeIn
                  $(this).fadeIn();
               }, 2000 * i);
            });
        });
        return false;
    });
});

Upvotes: 0

Ron Gilchrist
Ron Gilchrist

Reputation: 909

See http://jsfiddle.net/5uNGR/15/

Try something where you are not trying to perform a fadeIn on each image as they load, but test the ok-ness of the next image in your animation queue each time ANY load event happens, then on the animation callback, see if the next one is ready. Here is a script that should work.

BTW, see http://www.sajithmr.me/javascript-check-an-image-is-loaded-or-not for more on image readiness testing.

$(function () {
    // util fn to test if image loaded properly
    var isImgLoadOk = function (img) {
        if (!img.complete) { return false; }
        if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
            return false;
        }
        return true;
    };

    $("a").on('click', function () {

        $("#load").load('load.html', function () {

            var imgs = $('#load img').hide(), //create image array and hide (chaining)
                animIndex = 0, //position in the animation queue
                isFading = false; //track state of whether an animation is in prog

            // this is a load handler AND is called in the fadeIn callback if 
            // not at last image in the collection
            var fadeNextImg = function () {

                // make sure a load event didn't happen in the middle of an animation
                if (isFading) return;

                // last image in animation queue?
                var isLast = animIndex == imgs.length - 1;

                // get the raw image out of the collection and test if it is loaded happily
                var targetImage = imgs[animIndex];
                if (isImgLoadOk(targetImage)) {
                    // jQuery-up the htmlImage
                    targetImage = $(targetImage);

                    // increment the queue
                    animIndex++;

                    // set animation state - this will ignore load events
                    isFading = true;

                    // fade in the img
                    targetImage.fadeIn('slow', function () {
                        isFading = false;
                        // test to see if next image is ready to load by 
                        // recursively calling the parent fn
                        if (!isLast) fadeNextImg();
                    });

                }
            };

            imgs.on('load', fadeNextImg);

        });

        return false;
    });

});

Upvotes: 1

Related Questions