-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (98 loc) · 2.79 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function wiatal(element, config) {
let i = 0, length;
// so that terser shrinks the names (more compression)
let set_timeout = setTimeout, set_interval = setInterval, clear_interval = clearInterval, error = console.error;
let fill_defaults = (data) => {
let defaults = {
strings: ['Put your strings here...', 'and Enjoy!'],
typeSpeed: 150,
backSpeed: 50,
backDelay: 500,
startDelay: 500,
loopDelay: 0,
cursorChar: "|",
placeholder: false,
showCursor: true,
disableBacktyping: false,
onFinished: () => {},
loop: true
};
data = data || {};
for (let i in data) {
defaults[i] = data[i];
}
return defaults;
};
let typewrite = () => {
let extra_delay = 0;
if (i == length) {
i = 0;
extra_delay = config.loopDelay;
}
set_timeout(() => { type_string(config.strings[i]); }, config.startDelay + extra_delay);
};
let type_string = (str) => {
let index = 0, str_l = str.length;
let interval = set_interval(() => {
if (config.placeholder) {
element.placeholder += str[index];
}
else {
element.textContent += str[index];
}
if (++index == str_l) {
return string_typed(interval);
}
}, config.typeSpeed);
};
let string_typed = (interval) => {
clear_interval(interval);
if ((config.disableBacktyping || !config.loop) && i == length - 1) {
return config.onFinished();
}
set_timeout(() => erase_string(), config.backDelay);
};
let erase_string = () => {
let str = config.placeholder ? element.placeholder : element.textContent;
let str_l = str.length;
let interval = set_interval(() => {
if (config.placeholder) {
element.placeholder = str.substr(0, --str_l);
}
else {
element.textContent = str.substr(0, --str_l);
}
if (str_l == 0) {
return string_erased(interval);
}
}, config.backSpeed);
};
let string_erased = (interval) => {
clear_interval(interval);
++i;
typewrite();
};
let set_cursor = () => {
let e = element;
let cursorSpan = document.createElement('span');
cursorSpan.classList.add('ityped-cursor');
cursorSpan.textContent = config.cursorChar;
e.insertAdjacentElement('afterend', cursorSpan);
};
let start_typing = () => {
if (!element) {
error("wiatal: no element defined");
return;
}
config = fill_defaults(config);
length = config.strings.length;
if (typeof element == "string") element = document.querySelector(element);
if (!element) {
error("wiatal: element does not exist");
return;
}
if (config.showCursor && !config.placeholder) set_cursor();
typewrite();
};
return start_typing();
}