-
Notifications
You must be signed in to change notification settings - Fork 0
/
typewriter.js
44 lines (38 loc) · 1.16 KB
/
typewriter.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
var span = document.querySelector(".typewriter span");
var textArr = span.getAttribute("data-text").split(", ");
var maxTextIndex = textArr.length;
var sPerChar = 0.15;
var sBetweenWord = 1.5;
var textIndex = 0;
typing(textIndex, textArr[textIndex]);
function typing(textIndex, text) {
var charIndex = 0;
var maxCharIndex = text.length - 1;
var typeInterval = setInterval(function () {
span.innerHTML += text[charIndex];
if (charIndex == maxCharIndex) {
clearInterval(typeInterval);
setTimeout(function () {
deleting(textIndex, text);
}, sBetweenWord * 1000);
} else {
charIndex += 1;
}
}, sPerChar * 1000);
}
function deleting(textIndex, text) {
var minCharIndex = 0;
var charIndex = text.length - 1;
var typeInterval = setInterval(function () {
span.innerHTML = text.substr(0, charIndex);
if (charIndex == minCharIndex) {
clearInterval(typeInterval);
textIndex + 1 == maxTextIndex ? (textIndex = 0) : (textIndex += 1);
setTimeout(function () {
typing(textIndex, textArr[textIndex]);
}, sBetweenWord * 1000);
} else {
charIndex -= 1;
}
}, sPerChar * 1000);
}