【发布时间】:2018-11-09 17:54:57
【问题描述】:
我正在为我的网站构建一个轮播(幻灯片)效果,但我没有什么问题。在每次图像更改时,我都想制作淡入淡出效果。所以我为它添加了一个带有动画的类。问题来了。
此函数每 3 秒触发一次 (setInterval)
let sliderInterval = setInterval(nextImg, 3000);
function nextImg() {
imgChange(sliderNum + 1);
}
const heroImg = document.querySelector('.hero__image');
function imgChange(x) {
heroImg.classList.remove("fade");
sliderNum = (x + imgLocations.length) % imgLocations.length;
heroImg.src = imgLocations[sliderNum];
heroImg.classList.add("fade");
}
淡化效果:
.fade {
animation: fade 1.5s ease-in-out;
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
<div class="hero">
<img class="hero__image" src="photo1">
</div>
它仅适用于第一个图像切换。尽管在函数开始时它应该删除类 fade 我在函数中看到它留在元素中并且不会消失。如果我尝试将此效果放在hero 部分或img 中,这并不重要。
【问题讨论】:
-
因为您没有等待动画完成甚至开始。您可以在相同的代码中直接添加该类。
-
也许有处理这个动画的解决方法?
标签: javascript css class animation