jQuery. fading in and fading out

jQuery-logo1.pngFading with jQuery is a nice technique that can be used to show some dynamic content such as xml feeds, some famous quotes, or photo gallery. The basic's of fading are simply to attach a fade to div, but what does that offer without updating the content of that div. So I am going to create a fade that updates once the fade is complete and add a delay on some pictures for a smooth image gallery.

View the gallery here.

The parameters that get put into our fade() are the duration of the fade and a callback. The duration can be written using the jQuery defined speeds of "slow", "normal" or "fast", but could be replaced with milliseconds value such as 1000 = 1 second or 10000 = 10 seconds.

Let's start with the ready function and add a fade to it.

    $(document).ready(
        function() {

            $('.imageBox img').fadeOut(10000, function() {

                   alert("Fade complete.")
            });
       }
    );

So what happens in above code is the div(imageBox) with the contents of image will have the image fade out in 10 seconds and produce an alert telling us the image is 100% faded. It works but of really no use or purpose. Now let's add more dynamic content.
Let's add an array of images and modify the fade to accept the array.

$(document).ready(
        function() {

            var i = 0; // var i for array increment
            var imgs = new Array(); // Create the array

            //Define array indices
            imgs[0] = "<img src='images/parise-goal.jpg' alt='' />";
            imgs[1] = "<img src='images/marty.jpg' alt='' />";
            imgs[2] = "<img src='images/gionta.jpg' alt='' />";
            imgs[3] = "<img src='images/goal.jpg' alt='' />";
            imgs[4] = "<img src='images/marty2.jpg' alt='' />";

   
            runit(imgs); // Call runIt function

            function runIt() {

                $('.imageBox').html(imgs[i]); // Render image array

                    $('.imageBox img').fadeOut(10000, function() {

                        setTimeout(runIt,'200'); // Set delay and re-start runIt function
                    });

                i = i + 1; // Increment i
                if (i == imgs.length) { i = 0; } restart from 0
        }

    }
);

What did I just do?
First I declared some variables along with a new function called runIt(). Inside of that function I handle the updating of the image(imgs[i]). Attached to the fadeOut is the callback function() where I use a setTimeout() for a short delay which calls the runIt() function after 200 milliseconds. Once the fadeOut is complete we increment the value of "i" and test the value of "i" to determine whether to restart the array at 0.

The setTimeout function allows us slow it all down which a for or each() loop would not allow us to do.

View the gallery here.


| | Comments (2) | TrackBacks (0)
Disco G said:

Hey I like the coding you have there but I can't get it to work. Did you set any visibility or display attributes to the css? Did you also link the images in the html as well? I think it would be a good idea to show a snippet of your html and css so people can get a good sense of what's going on in the other files.

mjahern Author Profile Page said:

Disco G:

The css is very minimal, I was more focused on the fade working properly. All other refinements could be made at one's own leisure.

The html for the images is written and handled by the script. In the script you could also write in a link or links. If you were to use just one link on all images, that could also be written into the HTML with a small change of the selector with the script.

(ex: html below)

(ex: script change below)

$('.imageBox a img').fadeOut(10000, function() {

Here's the css:

.box {
height:700px;
text-align:center;
}

.imageBox img {

padding:7px;
border:5px solid #ffffff;
}

I hope that helps. You can also email me your code if you need the help.

Leave a comment

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda,bmw bmw bmw Acura, Honda, Acura Forerunner, SUV, BMW, Mercedes, Toyota  
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota


Chicago Blackhawks
Chicago Blackhawks
Stanley Cup
Stanley Cup
Pittsburgh Penguins
Pittsburgh Penguins
Boston Bruins
Boston Bruins
Fire & Flames
Fire & Flames








 


Categories

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda, Acura, Honda, Acura Forerunner,bmw bmw bmw SUV, BMW, Mercedes, Toyota