I have two images stacked. With setInterval(), the top one fades away, exposing the second. Then the top image switches to the bottom's src while invisible, and becomes opaque again. The second image changes to the next image and waits for the setInterval() to fade the top image and do it all over again.
It all works great, except the first time around; There's no fade. What am I not seeing?
This happens on all firefox and chrome and I assume all others.
HTML
<script type="text/javascript"> setInterval('swapImage()', 5000); var galleryCount = 3; </script> ... <img id="image" src="images/gallery/01.jpg" /> <img id="imagenext" src="images/gallery/02.jpg" /> Javascript
function swapImage(imageToFadeID) { $("#image").animate ( { "opacity": "0" }, "slow", "linear", changeImage() ); }; var i = 1; function changeImage() //counter +1 { i = i + 1; //add "0" to image number "j" if less than 10. if (i < 10) { var j = "0" + i; } else { j = i; } //change top image to match bottom var topImage = document.getElementById("imagenext").src; document.getElementById("image").src = topImage; //make top image reappear document.getElementById("image").style.opacity = '1'; //change out bottom image to next var btmImage = "images/gallery/" + j + ".jpg"; document.getElementById("imagenext").src = btmImage; //reset counter if at end if (i > galleryCount - 1) { i = 0; } }