sirenleader sirenleader - 1 year ago 98
CSS Question

CSS3 Slide Show Fade Effect not working

The code works just fine on JSFiddle, but I cant get it to work when I use it in a HTML/CSS file. Only the 50x200 image is displayed, no signs of the simple slideshow or fade effect. I work in Sublime text, could that create any problems?

var imgArray = [
curIndex = 0;
imgDuration = 3000;

function slideShow() {
document.getElementById('slider').className += "fadeOut";
setTimeout(function() {
document.getElementById('slider').src = imgArray[curIndex];
document.getElementById('slider').className = "";
if (curIndex == imgArray.length) { curIndex = 0; }
setTimeout(slideShow, imgDuration);

#slider {
transition: opacity 1s;

#slider.fadeOut {

<img id="slider" src="">

Answer Source

JSFiddle executes the javascript code in the window.onload event. You can change this if you click the JavaScript Button in the editor of JSFiddle. If you change it to No wrap - in <head> you'll see that it doesn't work as well. You should see an error in your console, telling you the reason.

I'm assuming that you're including your script snippet in the head section of your HTML Document.

If you take your code as posted in your question, your slider isn't loaded yet, because the script is executed before your HTML document is fully loaded. You have to wrap the call to your slideShow function inside the onloadevent (or if you're using jQuery you'll probably use $(document).ready(function(){ ... }) instead.

This should do the trick then:

window.onload = function() {

Including the script at the bottom of your HTML document should work as well as an alternative.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download