fairly new to Javascript. I have a text sliding up animation. The first div seems to work, but the second div doesn't. I have two divs but only one seems to work. Any suggestions/help on what seems to be wrong?
Here's my code below:
const txts = document.querySelector(".animate-text").children,
txtsLen = txts.length;
let index = 0;
const textInTimer = 3000,
textOutTimer = 2800;
function animateText() {
for (let i = 0; i < txtsLen; i++) {
txts[i].classList.remove("text-in", "text-out");
}
txts[index].classList.add("text-in");
setTimeout(function () {
txts[index].classList.add("text-out");
}, textOutTimer);
setTimeout(function () {
if (index == txtsLen - 1) {
index = 0;
} else {
index++;
}
animateText();
}, textInTimer);
}
window.onload = animateText;
.home .tree p {
margin: 0;
overflow: hidden;
}
.home .tree p span {
font-size: 2.8vw;
color: grey;
font-weight: 500;
display: none;
}
.home .tree p span.text-in {
display: block;
animation: textIn 0.5s ease;
}
.home .tree p span.text-out {
animation: textOut 0.5s ease;
}
@keyframes textIn {
0% {
transform: translateY(100%);
}
100% {
transform: translateY(0%);
}
}
@keyframes textOut {
0% {
transform: translateY(0%);
}
100% {
transform: translateY(-100%);
}
}
<div class="home">
<div class="tree">
<p class="animate-text">
<span class="">first</span>
<span class="">second</span>
<span class="text-in">third</span>
<span class="">forth</span>
<span class="">fifth</span>
</p>
</div>
</div>
<div class="home">
<div class="tree">
<p class="animate-text">
<span class="">first</span>
<span class="">second</span>
<span class="text-in">third</span>
<span class="">forth</span>
<span class="">fifth</span>
</p>
</div>
</div>
I tried the getElementByClassName selector but none of the suggestions seems to work that I've searched for here.
`, and that will only find the first such `
` on the page.
– Pointy Jul 21 '23 at 00:38tags in the animate-text class?
– Caramelpros Jul 21 '23 at 00:41