Hi, hoe you're well.
The code creates an effect where the letters of a text are displayed one by one with a fade-in animation. :)
Let's break down what this JS code is doing. First, we select the target element using querySelector
and then obtain the text content of the element, which is then split into an array of individual characters using the split
method. We then clear the text content of the target element.
Next, we loop through the array of characters and wrap each character in a span element with a class of "char". We add this HTML to the target element using innerHTML
.
Now, we set two variables: "char" represents the index of the current character being displayed, and "timer" holds the ID of the timer that will trigger the animation. We use setInterval
to call the "onTick" function every 60 milliseconds.
The "onTick" function selects the span element corresponding to the current character using querySelectorAll
. It adds a class of "Fadein" to the span element, triggering the fade-in animation. The "char" variable is then incremented.
Finally, we check if all characters have been displayed by comparing "char" to the length of the "splitText" array. If all characters have been displayed, we clear the timer using clearInterval
.
And that's it! With this code, you can create a typewriter effect with a fade-in animation in JavaScript. Feel free to customize the code to fit your needs.
Hope you enjoyed it!
Matane!