当我尝试构建一个简单的文本轮播时,我遇到了一个我很难理解的问题。
布局很简单。我有一个包装器和我希望轮播循环播放的文本。
然而,据我所知,我遇到的问题似乎来自 setInterval 方法。动画循环遍历所有文本并返回到开头后,显示的第一个和第二个文本之间存在奇怪的重叠。第一个文本将动画化,但随后将返回以暂时替换第二个文本。
任何帮助理解导致此错误以这种方式呈现的原因将不胜感激。
let animateSlide = setInterval(moveSlide, 1200);
function moveSlide() {
let carousel = document.getElementById("wordCarousel");
let firstSlide = carousel.children[0];
let createID = document.createAttribute("id");
createID.value = "active";
firstSlide.setAttributeNode(createID);
carousel.appendChild(carousel.firstChild);
carousel.children[carousel.children.length - 1].removeAttribute("id");
}Run Code Online (Sandbox Code Playgroud)
/* Carousel Styles */
#wordCarousel {
height: 36px;
overflow: hidden;
}
.carouselSlide {
color: #555;
font-size: 36px;
}
#active {
margin-top: 0px;
animation-name: example;
animation-duration: 1.2s;
animation-timing-function: ease;
}
@keyframes example {
from {
margin-top: 0px;
}
to {
margin-top: -40px;
}
}Run Code Online (Sandbox Code Playgroud)
<div id="wordCarousel"> …Run Code Online (Sandbox Code Playgroud)