-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotating-text.ts
40 lines (37 loc) · 1.18 KB
/
rotating-text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
let words = document.querySelectorAll(".word");
words.forEach(word => {
let letters = word.textContent.split("");
word.textContent = "";
letters.forEach(letter => {
let span = document.createElement("span");
span.textContent = letter;
span.className = "letter";
word.append(span);
});
});
let currentWordIndex = 0;
let maxWordIndex = words.length - 1;
(words[currentWordIndex] as HTMLElement).style.opacity = "1";
let rotateText = () => {
let currentWord = words[currentWordIndex];
let nextWord =
currentWordIndex === maxWordIndex ? words[0] : words[currentWordIndex + 1];
// rotate out letters of current word
Array.from(currentWord.children).forEach((letter, i) => {
setTimeout(() => {
letter.className = "letter out";
}, i * 80);
});
// reveal and rotate in letters of next word
(nextWord as HTMLElement).style.opacity = "1";
Array.from(nextWord.children).forEach((letter, i) => {
letter.className = "letter behind";
setTimeout(() => {
letter.className = "letter in";
}, 340 + i * 80);
});
currentWordIndex =
currentWordIndex === maxWordIndex ? 0 : currentWordIndex + 1;
};
rotateText();
setInterval(rotateText, 4000);