FlipFloop FlipFloop - 1 month ago 10
Javascript Question

Why won't my images fade in and out?

So I am currently working on a slideshow project in jQuery where images will fade in and out. The problem is that this only works on my KhanAcademy project, and not on my CodePen - Pen.

Can someone please tell me the issue on my CodePen? Thank you!

Code:



var slideShow = function(container, time, effect) {
container = document.querySelector(container);
this.images = [];
this.curImage = 0;

if (effect === "fade") {
for (i = 0; i < container.childElementCount; i++) {
this.images.push(container.children[i]);
this.images[i].style.opacity = 0;
}

// Handle going to to the next slide
var nextSlide = function() {
for (var i = 0; i < this.images.length; i++) {
if (i != this.curImage) this.images[i].style.opacity = 0;
}
this.images[this.curImage].style.opacity = 1;
this.curImage++;
if (this.curImage >= this.images.length) {
this.curImage = 0;
}
window.setTimeout(nextSlide.bind(document.getElementById(this)), time);
};
nextSlide.call(this);
} else if (effect === "clickFade") {
for (i = 0; i < container.childElementCount; i++) {
this.images.push(container.children[i]);
this.images[i].style.opacity = 0;
}

// Handle going to to the next slide
var nextSlideClick = function() {
for (var i = 0; i < this.images.length; i++) {
if (i != this.curImage) this.images[i].style.opacity = 0;
}
this.images[this.curImage].style.opacity = 1;
this.curImage++;

if (this.curImage >= this.images.length) {
this.curImage = 0;
}
window.setTimeout(nextSlideClick.bind(document.getElementById(this)), time);
};
nextSlideClick.call(this);
}

};
slideShow(".slideshow", 2000, "fade");

h1 {
font-family: 'Montserrat', sans-serif;
}

.slide {
transition: opacity 0.5 s;
position: absolute;
top: 1;
}

<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">

<h1>Exatreo.js - Slideshow library</h1>

<div class="slideshow">
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/butterfly.png" alt="Butterfly" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
<img class="slide" src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />

</div>




Answer

If you will check your css with the validator you will get this error:

0.5 is not a transition value : opacity 0.5 s

And you can see that you have a space between the 0.5 and the s.
Remove this space and it will work as expected.

var slideShow = function(container, time, effect) {
  container = document.querySelector(container);
  this.images = [];
  this.curImage = 0;

  if (effect === "fade") {
    for (i = 0; i < container.childElementCount; i++) {
      this.images.push(container.children[i]);
      this.images[i].style.opacity = 0;
    }

    // Handle going to to the next slide
    var nextSlide = function() {
      for (var i = 0; i < this.images.length; i++) {
        if (i != this.curImage) this.images[i].style.opacity = 0;
      }
      this.images[this.curImage].style.opacity = 1;
      this.curImage++;
      if (this.curImage >= this.images.length) {
        this.curImage = 0;
      }
      window.setTimeout(nextSlide.bind(document.getElementById(this)), time);
    };
    nextSlide.call(this);
  } else if (effect === "clickFade") {
    for (i = 0; i < container.childElementCount; i++) {
      this.images.push(container.children[i]);
      this.images[i].style.opacity = 0;
    }

    // Handle going to to the next slide
    var nextSlideClick = function() {
      for (var i = 0; i < this.images.length; i++) {
        if (i != this.curImage) this.images[i].style.opacity = 0;
      }
      this.images[this.curImage].style.opacity = 1;
      this.curImage++;

      if (this.curImage >= this.images.length) {
        this.curImage = 0;
      }
      window.setTimeout(nextSlideClick.bind(document.getElementById(this)), time);
    };
    nextSlideClick.call(this);
  }

};
slideShow(".slideshow", 2000, "fade");
h1 {
  font-family: 'Montserrat', sans-serif;
}

.slide {
    transition: opacity 0.5s;
    position: absolute;
    top: 1;
}
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">

<h1>Exatreo.js - Slideshow library</h1>

<div class="slideshow">
    <img class="slide" src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
    <img class="slide" src="https://www.kasandbox.org/programming-images/animals/butterfly.png" alt="Butterfly" />
    <img class="slide" src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
    <img class="slide" src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
    <img class="slide" src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />

</div>