Search code examples
javascripthtmlslideshowfading

How to add a fading effect for my JS slideshow?


I have this slideshow JS code that switches images in a div depending on the time we want. However, how can I add a fade in/fade out effect instead of a basic transition with no animation? I am doing this without jQuery, just plain javascript. Here is the link: https://en.khanacademy.org/computer-programming/js-library-exatreojs-slideshow-library/2950604004

here is the code:

var slideShow = function(container, time) {
            container = document.getElementById(container);
            this.images = [];
            this.curImage = 0;
            for (i = 0; i < container.childElementCount; i++) {
                this.images.push(container.children[i]);
                this.images[i].style.display = "none";
            }

            // Handle going to to the next slide
            var nextSlide = function() {
                for (var i = 0; i < this.images.length; i++) {
                    this.images[i].style.display = "none";
                }
                this.images[this.curImage].style.display = "block";
                this.curImage++;
                if (this.curImage >= this.images.length) {
                    this.curImage = 0;
                }
                window.setTimeout(nextSlide.bind(document.getElementById(this)), time);
                // old code: window.setTimeout(nextSlide.bind(this), time);
            };

            nextSlide.call(this);
        
        };
        slideShow("slideshow", 1000);
        // old code: slideShow(document.getElementById("slideshow"), 1000);
    <div id="slideshow">
        <img src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
        <img src="https://www.kasandbox.org/programming-images/animals/butterfly.png" alt="Butterfly" />
        <img src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
        <img src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
        <img src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />

    </div>


Solution

  • You could try this method, if you don't mind that display is always block and I just change opacity of image. So the container has to be relatively positioned and imgs should be absolute

    var slideShow = function(container, time) {
      container = document.getElementById(container);
      this.images = [];
      this.curImage = 0;
      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);
        // old code: window.setTimeout(nextSlide.bind(this), time);
      };
    
      nextSlide.call(this);
    
    };
    slideShow("slideshow", 1000);
    // old code: slideShow(document.getElementById("slideshow"), 1000);
    img {
      transition: opacity 0.5s;
      position:absolute;
      top:0;
    }
    <div id="slideshow">
      <img src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
      <img src="https://www.kasandbox.org/programming-images/animals/butterfly.png" alt="Butterfly" />
      <img src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
      <img src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
      <img src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />
    
    </div>