-
Notifications
You must be signed in to change notification settings - Fork 0
/
RevealingText.js
47 lines (40 loc) · 1.2 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
class RevealingText {
constructor(config) {
this.element = config.element
this.text = config.text
this.speed = config.speed || 70
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 => {
let span = document.createElement("span")
span.textContent = character
this.element.appendChild(span)
characters.push({
span,
delayAfter: character === " " ? 0 : this.speed
})
})
this.revealOneCharacter(characters)
}
}