-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReavalingText.js
48 lines (41 loc) · 1.22 KB
/
ReavalingText.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
class ReavalingText {
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(){
this.isDone = true;
this.element.querySelectorAll("span").forEach(s =>{
s.classList.add("revealed");
})
}
init() {
let characters = [];
this.text.split("").forEach(char => { // Используем другое имя для параметра
let span = document.createElement("span");
console.log(char);
span.textContent = char;
this.element.appendChild(span);
characters.push({
span,
delayAfter: char === " " ? 0 : this.speed
});
});
this.revealOneCharacter(characters);
}
}