gpt4 book ai didi

javascript - 使用javascript淡出幻灯片

转载 作者:搜寻专家 更新时间:2023-10-31 08:14:59 25 4
gpt4 key购买 nike

如标题所述,我想在我的幻灯片中的图像仅使用 html、css 和 javascript 交换后添加淡出动画。我不确定如何做到这一点,但我有一些想法。我在想我可以在当前图像上添加一个 id,比如#fadeout,这样它在淡出时就可以获得特定的特征。

var myIndex = 0;
window.onload = slidePictures();

function slidePictures() {
var i;
var slides = document.getElementsByClassName("mySlides");

for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
slides[i].setAttribute("id", "fadeout");

}
myIndex++;
if (myIndex > slides.length) {
myIndex = 1
}
slides[myIndex - 1].style.display = "block";
document.getElementById("indicator").innerHTML = myIndex + "/" + slides.length;

setTimeout(slidePictures, 3000);

}
.slidesDiv>img {
width: 80%;
height: 80%;
margin-left: 10%;
opacity: 1;
transition: opacity 1s;
}

#fadeOut {
opacity: 0;
}
<div class="slidesDiv">
<img class="mySlides" src="//placehold.it/200x80/0fb">
<img class="mySlides" src="//placehold.it/200x80/0bf">
<img class="mySlides" src="//placehold.it/200x80/fb0">
<img class="mySlides" src="//placehold.it/200x80/0fb">
<h1 id="indicator"> Indicator </h1>
</div>

最佳答案

  1. 你不应该display属性(property) transition ,因为它不是 animatable .
  2. 我推荐使用 position: absolute并在 init 上隐藏元素。
  3. 我认为结合淡入/淡出效果更好。

试试这个例子:

var indexes = {current: 0};
var slides = document.getElementsByClassName('mySlides');
window.onload = slidePictures();

function slidePictures() {
if (indexes.last) {
slides[indexes.last].classList.remove('visible');
}

slides[indexes.current].classList.add('visible');

document.getElementById('indicator').innerHTML = (indexes.current + 1) + '/' + slides.length;

indexes.last = indexes.current;
indexes.current++;
if (indexes.current >= slides.length) {
indexes.current = 0;
}

setTimeout(slidePictures, 3000);
}
html, body {
width: 100%;
height: 100%;
}

.slidesDiv {
width: 80%;
height: 80%;
margin-left: 10%;
position: relative;
}

.mySlides {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: all 1s;
}

.visible {
opacity: 1;
}
<div class="slidesDiv">
<img class="mySlides" src="//placehold.it/200x80/0fb">
<img class="mySlides" src="//placehold.it/200x80/0bf">
<img class="mySlides" src="//placehold.it/200x80/fb0">
<img class="mySlides" src="//placehold.it/200x80/bbb">
</div>
<h1 id="indicator">1/4</h1>

JSFiddle

关于javascript - 使用javascript淡出幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42313943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com