-
Notifications
You must be signed in to change notification settings - Fork 2
/
helloworld.js
48 lines (43 loc) · 1.77 KB
/
helloworld.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
// Helloworld extension, https://github.com/schulle4u/yellow-helloworld
var initHelloworldFromDOM = function() {
// Animate element
var animateElement = function() {
var message = elements[elementIndex].getAttribute("data-message");
var speed = elements[elementIndex].getAttribute("data-speed");
if (elementPosition<=message.length) {
elements[elementIndex].innerHTML = message.substring(0, elementPosition)+"█";
elementPosition++;
setTimeout(animateElement, speed);
} else if (elementPosition<=message.length+6) {
var cursor = (elementPosition-message.length)%2 ? "█" : " ";
elements[elementIndex].innerHTML = message.substring(0, elementPosition)+cursor;
elementPosition++;
setTimeout(animateElement, speed*3);
} else {
elementPosition = 0;
elementIndex++;
if (elementIndex>=elements.length) elementIndex = 0;
if (elementIndex) setTimeout(animateElement, speed);
}
};
// Handle click on element
var onClickElement = function(e) {
e.stopPropagation();
e.preventDefault();
if (elementIndex==0 && elementPosition==0) {
for (var i=0, l=elements.length; i<l; i++) {
elements[i].innerHTML = " ";
}
animateElement();
}
};
// Initialise animation loop
var elementIndex = 0;
var elementPosition = 0;
var elements = document.querySelectorAll(".helloworld");
for (var i=0, l=elements.length; i<l; i++) {
elements[i].onclick = onClickElement;
}
if (elements.length) animateElement();
};
window.addEventListener("DOMContentLoaded", initHelloworldFromDOM, false);