如何向此幻灯片脚本添加不透明度过渡?第2部分

时间:2017-08-08 09:16:50

标签: javascript html css

再次问。我已经取得了一些进展,但不幸的是我仍然无法完成它,而且我有点绝望。

我昨天提出的问题可以在这里找到:How can I add opacity transition to this script?

正如安东尼所说,我试图给出明显的" mySlides"一个名为.visible的额外类,当调用下一个img时将其删除。

经过一些研究后,我设法给它新的类:x [slideIndex-1] .classList.add(" visible")

但过渡仍然没有成功。

我做错了什么?



var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i,
    x = document.getElementsByClassName("mySlides"),
    dots = document.getElementsByClassName("demo");

  if (n > x.length) {
    slideIndex = 1
  }

  if (n < 1) {
    slideIndex = x.length
  }

  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
    x[i].classList.remove("visible");
  }

  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" w3-white", "");
  }

  x[slideIndex - 1].style.display = "block";
  x[slideIndex - 1].classList.add("visible")
  dots[slideIndex - 1].className += " w3-white";
}
&#13;
img.mySlides {
  opacity: 0;
  transition: opacity 2s;
}

img.mySlides.visible {
  opacity: 1;
  transition: opacity 2s
}
&#13;
<div class="w3-content w3-display-container">
  <div id="slider-wrapper">
    <img id="zupfinstrumente" class="centerscreen mySlides slidepos" src="img1.jpg" alt="xy">
    <img id="ueber-mich" class="centerscreen mySlides slidepos" src="img2.jpg" alt="xy">
    <img id="streichinstrumente" class="centerscreen mySlides slidepos" src="img3.jpg" alt="xy"></div>
  <div class="w3-center w3-container w3-section w3-large w3-text-white centerscreen badgepos">
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
    <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
  </div>
  <container id="buttons" class="centerscreen">
    <div id="navarrow-left" class="w3-left w3-hover-text-black btn" onclick="plusDivs(-1)">&#10094;</div>
    <div id="navarrow-right" class="w3-right w3-hover-text-black btn" onclick="plusDivs(1)">&#10095;</div>
  </container>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你可以创建你的惯用css annimation,然后当你的img wo显示动画的图像时,最后使用它;

annimation css代码:

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

见下面的代码:

var slideIndex = 1;
	showDivs(slideIndex);

function plusDivs(n) {
	showDivs(slideIndex += n);
}

function currentDiv(n) {
	showDivs(slideIndex = n);
}

function showDivs(n) {
	var i,
		x = document.getElementsByClassName("mySlides"),
		dots = document.getElementsByClassName("demo");
	
	if (n > x.length) {slideIndex = 1
	} 
	
	if (n < 1) {slideIndex = x.length
	}
	
	for (i = 0; i < x.length; i++) {
		x[i].style.display = "none";
	}
	
	for (i = 0; i < dots.length; i++) {
	  dots[i].className = dots[i].className.replace(" w3-white", "");
	}
	 
	x[slideIndex-1].style.display = "block";
	dots[slideIndex-1].className += " w3-white";
}
.mySlides {display:none}
.w3-left, .w3-right, .w3-badge {cursor:pointer}
.w3-badge {height:13px;width:13px;padding:0}


/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .1} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .1} 
  to {opacity: 1}
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"/>
<div class="w3-content w3-display-container">
    <div id="slider-wrapper">
        <img id="zupfinstrumente" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_mountains_wide.jpg" alt="xy">
        <img id="ueber-mich" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_fjords_wide.jpg" alt="xy">
        <img id="streichinstrumente" class="centerscreen mySlides slidepos fade" src="https://www.w3schools.com/w3css/img_nature_wide.jpg" alt="xy"></div>
    <div class="w3-center w3-container w3-section w3-large w3-text-white centerscreen badgepos">
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(1)"></span>
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(2)"></span>
        <span class="w3-badge demo w3-border w3-transparent w3-hover-white" onclick="currentDiv(3)"></span>
    </div>
    <container id="buttons" class="centerscreen">
        <div id="navarrow-left" class="w3-left w3-hover-text-black btn" onclick="plusDivs(-1)">&#10094;</div>
        <div id="navarrow-right" class="w3-right w3-hover-text-black btn" onclick="plusDivs(1)">&#10095;</div>
    </container>
</div>