-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRevealingText.js
52 lines (42 loc) · 1.11 KB
/
RevealingText.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
class RevealingText {
constructor(config) {
this.element = config.element;
this.text = config.text;
this.speed = config.speed || 60;
this.timeout = null;
this.isDone = false;
}
revealOneCharacter(list) {
const next = list.splice(0, 1)[0];
next.span.classList.add("revealed");
if (list.length > 0) {
this.timeout = setTimeout(() => {
this.revealOneCharacter(list)
}, next.delayAfter)
} else {
this.isDone = true;
}
}
warpToDone() {
clearTimeout(this.timeout);
this.isDone = true;
this.element.querySelectorAll("span").forEach(s => {
s.classList.add("revealed");
})
}
init() {
let characters = [];
this.text.split("").forEach(character => {
//Create each span, add to element in DOM
let span = document.createElement("span");
span.textContent = character;
this.element.appendChild(span);
//Add this span to our internal state Array
characters.push({
span,
delayAfter: character === " " ? 0 : this.speed
})
})
this.revealOneCharacter(characters);
}
}