I am trying to get images in a slideshow to crossfade by using CSS3 transitions. I have the slideshow working and I know how to use transitions, but I don’t know how to get the CSS and Javascript to work together to accomplish this. I am relatively new to Javascript so simplified answers are greatly appreciated.
Javascript:
var images = new Array()
images[0] = "img/1.jpg";
images[1] = "img/2.jpg";
images[2] = "img/3.jpg";
images[3] = "img/4.jpg";
images[4] = "img/5.jpg";
var timer = setInterval(checkImage, 3000);
var x=0;
function checkImage()
{
if (x>4)
{
x=0;
changeImage();
}
else
{
changeImage();
}
}
function changeImage()
{
document.getElementById("slideimg").src=images[x]
x++;
}
HTML:
<img src="img/5.JPG" width="400" height="300" id="slideimg">
Since you’re asking for the whole thing including CSS, here’s a working demo (requires CSS3 transition-capable browser such as Chrome, Safari or Firefox 4+): http://jsfiddle.net/jfriend00/cwP5Q/.
HTML:
CSS:
JS (runs when page is ready):
If you were going to do this for real, you should use a class library like jQuery or YUI which will make animations both easier and work in all browsers, not just CSS3 capable browsers.