-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (57 loc) · 1.64 KB
/
index.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
document.addEventListener("DOMContentLoaded", () => {
animateHeader();
animateTabs();
});
function animateTabs() {
const allTabs = document.querySelectorAll(".tab");
let index = 0;
const interval = setInterval(function () {
if (index < allTabs.length) {
const tab = allTabs.item(index);
tab.classList.remove("initial");
index++;
} else {
clearInterval(interval);
}
}, 400);
}
function animateHeader() {
const element = document.querySelector("#name");
const iterationsBeforeFinish = 4;
let target = "Joshua Beighton";
let elapsed = 0;
const interval = setInterval(function () {
replaceLastChar(element);
elapsed++;
if (elapsed >= iterationsBeforeFinish) {
setLastChar(element, target.charAt(element.innerHTML.length - 1));
elapsed = 0;
if (element.innerHTML.length >= target.length) {
clearInterval(interval);
} else {
element.innerHTML += getRandomChar();
}
}
}, 10);
}
function replaceLastChar(element) {
let text = element.innerHTML;
text = text.substring(0, text.length - 1);
text += getRandomChar();
element.innerHTML = text;
}
function setLastChar(element, char) {
let text = element.innerHTML;
text = text.substring(0, text.length - 1);
text += char;
element.innerHTML = text;
}
function getRandomChar() {
let number = Math.floor(Math.random() * 26);
if (Math.random() > 0.5) {
number += 65;
} else {
number += 97;
}
return String.fromCharCode(number);
}