jQuery. fading in and fading out
Fading 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.
$(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.
Leave a comment
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
2whoa.com - NEW!!
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota



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.
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.