forked from Grow-with-Open-Source/Javascript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (35 loc) · 1.18 KB
/
script.js
File metadata and controls
38 lines (35 loc) · 1.18 KB
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
const typedText = document.querySelector('.typed-text');
const cursor = document.querySelector('.cursor');
const words = ["Versatile", "Cool", "Life", "Fun", "Awesome", "Fast", "Famous"];
const typingDelay = 100;
const erasingDelay = 100;
const newLetterDelay = 1000;
let index = 0;
let charIndex = 0;
document.addEventListener("DOMContentLoaded", () => {
if (words.length) {
setTimeout(typeText, newLetterDelay);
}
});
function typeText() {
if (charIndex < words[index].length) {
typedText.textContent += words[index].charAt(charIndex);
charIndex++;
setTimeout(typeText, typingDelay);
} else {
setTimeout(erase, newLetterDelay);
}
}
function erase() {
if (charIndex > 0) {
typedText.textContent = words[index].substring(0, charIndex - 1);
charIndex--;
setTimeout(erase, erasingDelay);
} else {
index++;
if (index >= words.length) {
index = 0;
}
setTimeout(typeText, newLetterDelay + 1000);
}
}