gpt4 book ai didi

javascript - 为什么此幻灯片中图像的不透明度没有改变? (CSS, Javascript)

转载 作者:行者123 更新时间:2023-12-04 07:38:31 24 4
gpt4 key购买 nike

我正在尝试构建图像轮播。但是,我遇到了不透明动画/过渡的意外行为。
我有一个 .active成功在三个之间转换的类 img .此 .active类(class)增加 opacity: 1; .然而,作为最后img加载到 DOM 中,它保留其初始 opacity: 0; ,即使 .active类被添加到每个 img每六秒,opacity不会改变。
我想这可能是 CSS 问题,但我感谢您的帮助!
Javascript

    const autoSlideshow = () => {
const slides = document.querySelectorAll('.slides img')
const slideDelay = 6000
let currentSlide = 0

slides[currentSlide].classList.add('.active')

const nextSlide = () => {
slides[currentSlide].classList.remove('.active')
currentSlide = (currentSlide + 1) % slides.length
slides[currentSlide].classList.add('.active')
}

setInterval(nextSlide, slideDelay)
}
autoSlideshow()
CSS
.slides img.active {
opacity: 1;
transition: opacity 2s ease-in-out;
}
.slides img {
position: inherit;
width: 100%;
height: 100%;
object-fit: cover;
opacity: 0;
}
HTML
<div class="slides">
<img src="img/ac62.jpg" alt="couple 1">
<img src="img/b348.jpg" alt="couple 2">
<img src="img/bk40.jpg" alt="couple 3">
</div>

最佳答案

解决方案

const autoSlideshow = () => {
const slides = document.querySelectorAll('.slides img')
const slideDelay = 6000
let currentSlide = 0

slides[currentSlide].classList.add('active')

const nextSlide = () => {
slides[currentSlide].classList.remove('active')
currentSlide = (currentSlide + 1) % slides.length
slides[currentSlide].classList.add('active')
}

setInterval(nextSlide, slideDelay)
}
autoSlideshow()
替换 .activeactive

关于javascript - 为什么此幻灯片中图像的不透明度没有改变? (CSS, Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67614024/

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