为什么我的图像不会淡入淡出?

时间:2016-11-07 19:57:28

标签: javascript performance slideshow codepen

因此,我目前正在使用jQuery中的幻灯片项目,其中图像将淡入淡出。问题是这只适用于我的KhanAcademy project,而不适用于CodePen - Pen

有人可以告诉我CodePen上的问题吗?谢谢!

代码:



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");
&#13;
h1 {
  font-family: 'Montserrat', sans-serif;
}

.slide {
    transition: opacity 0.5 s;
    position: absolute;
    top: 1;
}
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

如果您使用the validator检查您的CSS,则会收到此错误:

0.5 is not a transition value : opacity 0.5 s

您可以看到0.5s之间有空格 删除此空格,它将按预期工作。

&#13;
&#13;
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");
&#13;
h1 {
  font-family: 'Montserrat', sans-serif;
}

.slide {
    transition: opacity 0.5s;
    position: absolute;
    top: 1;
}
&#13;
<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>
&#13;
&#13;
&#13;