【问题标题】:How to add a fading effect for my JS slideshow?如何为我的 JS 幻灯片添加渐变效果?
【发布时间】:2016-03-05 16:39:39
【问题描述】:

我有这个幻灯片 JS 代码,它可以根据我们想要的时间在 div 中切换图像。但是,如何添加淡入/淡出效果而不是没有动画的基本过渡?我在没有 jQuery 的情况下这样做,只是普通的 javascript。这是链接: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>

【问题讨论】:

  • 要添加淡入淡出,您需要使用从 0 到 1 的计时器逐渐增加新图像的不透明度。因此可能需要绝对定位两个图像,一个具有较低 z- index 正在被另一个具有更高 z-index 的图像替换,并且在此图像上将是不透明动画。
  • 谢谢!让我走上正轨!
  • 另外,您可能希望通过一个包含对实际代码的 setTimeout 调用的循环进行渐进淡入/淡出。在循环的每次迭代中, setTimeout 在调用执行淡入淡出的函数之前创建一个小的延迟。这样,不透明度的变化不会一下子发生。

标签: javascript html slideshow fading


【解决方案1】:

你可以试试这个方法,如果你不介意显示总是阻塞,我只是改变图像的不透明度。所以容器必须相对定位并且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>

【讨论】:

  • 是否可以做一个“选项”,如果我想要淡入淡出吗?
  • 当然。通过transition实现的这个淡入淡出效果:0.5s。因此,您可以创建将添加带有转换 0 的类的按钮;到幻灯片中的每个元素,不会有淡入淡出效果
  • 我明白了(不要试图听起来专横/刻薄或任何东西,我喜欢你所做的,它真的对我的项目有帮助),但是例如,如果我想像滑动动画一样添加,我怎样才能实现它?
  • 如果你想要不同类型的动画,你可能应该考虑使用一些现有的库。但是,如果您想从头开始制作所有东西,那么可以。为此,您应该添加到 css tansition:.5s 更改 style.transformX 而不是不透明度。将它设置为“100%”用于您想要滑开的块,将其设置为 0 用于所有其他
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-20
  • 2014-03-05
  • 2018-03-11
  • 2016-10-27
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多