So I'm trying to do this vanilla text animation with JS and CSS but when I apply it to more that one text it work only on the fist one. I follow this tutorial: https://www.youtube.com/watch?v=GUEB9FogoP8&t=687s
It works but only on one text element. I saw the same animation on a portfolio of https://www.fromluke.com Which is even cleaner, but he used a different method.
Thanks for your tips!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="text-test.css">
</head>
<body>
<div class="c-contaier">
<di class="first">
<h1 class="fancy">Hi,I'm</h1>
</di>
<h1 class="fancy">Super Mario</h1>
</div>
<script src="app.js"></script>
</body>
</html>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family:'Outfit', sans-serif;
}
body {
background-color:white;
}
h1{
color:black;
font-size: 140px;
text-align:left;
font-weight: 500;
text-transform: uppercase;
overflow: hidden;
}
span{
opacity: 0;
transform: translateY(200px);
transition: all 0.8s ease;
display:inline-block;
}
span.fade{
opacity: 1;
transform: translateY(0px);
}
.space{
width:24px;
height: 5px;
position: relative;
}
.c-container {
max-width: 1920px;
margin-right: auto;
margin-left: auto;
padding-right: 4rem;
padding-left: 4rem;
}
const text = document.querySelector('.fancy');
const textString = text.textContent;
const splitText = textString.split("");
text.textContent = "";
for (let i=0; i < splitText.length; i++) {
if (splitText[i] === " ") {
text.innerHTML += "<span class='space'>" + splitText[i] + "</span>";
} else {
text.innerHTML += "<span>" + splitText[i] + "</span>";
}
}
let char = 0;
let timer = setInterval(onTick, 40);
function onTick() {
if (char < splitText.length) {
const span = text.querySelectorAll('span')[char];
span.classList.add('fade');
char++;
if (char === splitText.length) {
complete();
return
}
}
}