如何为我的 JS 幻灯片添加淡入淡出效果?

How to add a fading effect for my JS slideshow?

我有这个幻灯片 JS 代码,可以根据我们想要的时间 div 切换图像。但是,如何添加淡入淡出 in/fade 效果而不是没有动画的基本过渡?我这样做没有 jQuery,只是简单 javascript。这是 link:https://en.khanacademy.org/computer-programming/js-library-exatreojs-slideshow-library/2950604004

代码如下:

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>

你可以试试这个方法,如果你不介意显示总是块,我只是改变图像的不透明度。所以容器必须相对定位并且imgs应该是绝对

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>