2

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

2 Answers 2

8

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:

<div id="container">
<img id="slideimg0" class="slide showMe" src="http://photos.smugmug.com/photos/344287800_YL8Ha-S.jpg">
<img id="slideimg1" class="slide" src="http://photos.smugmug.com/photos/344287888_q22cB-S.jpg">
<img id="slideimg2" class="slide" src="http://photos.smugmug.com/photos/344284440_68L2K-S.jpg">
<img id="slideimg3" class="slide" src="http://photos.smugmug.com/photos/344286315_oyxRy-S.jpg">
<img id="slideimg4" class="slide" src="http://photos.smugmug.com/photos/344285236_hjizX-S.jpg">
</div>

CSS:

#container {position: relative; font-size: 0;}
.slide {
    border: none; 
    opacity: 0; 
    position: absolute; 
    top: 0; 
    left: 0;
    -webkit-transition: opacity 2s linear;
    -moz-transition: opacity 2s linear;
    -o-transition: opacity 2s linear;
    transition: opacity 2s linear;
}
.showMe {opacity: 1;}

JS (runs when page is ready):

var timer = setInterval(nextImage, 4000);
var curImage = 0;
var numImages = 5;

function nextImage() {
    var e;
    // remove showMe class from current image
    e = document.getElementById("slideimg" + curImage);
    removeClass(e, "showMe");

    // compute next image
    curImage++;
    if (curImage > numImages - 1) {
        curImage = 0;
    }

    // add showMe class to next image
    e = document.getElementById("slideimg" + curImage);
    addClass(e, "showMe");
}

function addClass(elem, name) {
    var c = elem.className;
    if (c) c += " ";  // if not blank, add a space separator
    c += name;
    elem.className = c;
}

function removeClass(elem, name) {
    var c = elem.className;
    elem.className = c.replace(name, "").replace(/   /g, " ").replace(/^ | $/g, "");  // remove name and extra blanks
}

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your alternate Javascript but it didn't work for me (the images didn't switch). Like I said, I'm new to Javascript so I wouldn't be surprised it was my fault. I don't currently have any CSS for this as I didn't know how to integrate it.
Since it sounds like you were asking for a whole working example including CSS, I've changed my answer to be that.
0

edit: I made a more extensible script than the one I previously posted the s() function switches to the slide number, the p variable is the play/pause. with that, you can easily make buttons to switch slides. I made the images be inside links because you probably want to be able to click the pictures to go somewhere.

<style>
div{position:relative;width:900px;height:350px;overflow:hidden;margin:0 auto;border:solid 10px #fff;box-shadow:0 0 10px rgba(0,0,0,0.4);background:#fff;margin-bottom:60px;}
div>a>img{opacity:0;position:absolute;top:0;left:0;width:900px;
-webkit-transition:opacity 1s linear;
-moz-transition:opacity 1s linear;
-ms-transition:opacity 1s linear;
-o-transition:opacity 1s linear;
transition:opacity 1s linear}
body{text-align:center}
</style>

<script>
var i=0,p=1,q=function(){return document.querySelectorAll("div>a>img")};

function s(e){
for(c=0;c<q().length;c++){q()[c].style.opacity="0";q()[e].style.opacity="1"}
}

setInterval(function(){
if(p){i=(i>q().length-2)?0:i+1;s(i)}
},5000);
</script>

<a href="javascript:p=1">play</a>
<a href="javascript:s(0);i=0;p=0">0</a>
<a href="javascript:s(1);i=1;p=0">1</a>
<a href="javascript:s(2);i=2;p=0">2</a>
<a href="javascript:s(3);i=3;p=0">3</a>
<a href="javascript:s(4);i=4;p=0">4</a>
<div>
<a><img src="Public/shot1.png" style="opacity:1"></a>
<a><img src="Public/shot2.png"></a>
<a><img src="Public/shot3.png"></a>
<a><img src="Public/shot4.png"></a>
<a><img src="Public/shot5.png"></a>
</div>​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.