-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchomp.js
More file actions
75 lines (60 loc) · 1.93 KB
/
chomp.js
File metadata and controls
75 lines (60 loc) · 1.93 KB
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
// <div class="tooth" style="height: {{tooth}}px" ng-repeat="tooth in bottomTeeth track by $index"></div>
var getTeeth = (num) => {
var teeth = '';
for (var i = 0; i < num; i++){
teeth += '<div class="tooth" style="height: ' + getRandomHeight() + 'px"></div>';
}
return teeth;
}
var getRandomHeight = () => {
return Math.floor(Math.random() * 5) + 6;
}
var getTemplate = (inputElem) => {
var width = inputElem.offsetWidth;
return `<div class="mouth" style="width: ` + (width + 18) + `px">
<div class="teeth top-teeth">` + getTeeth(10) + `
</div>
<div class="teeth bottom-teeth">` + getTeeth(8) + `
</div>
</div>
</div>`
}
var chomp = (event) => {
var element = event.target;
var mouth = element.parentElement;
if (!element.value) {
return;
}
setTimeout(function(){
mouth.classList.add('closed');
},500)
setTimeout(function(){
mouth.classList.add('open');
mouth.classList.remove('closed');
},600)
setTimeout(function(){
mouth.classList.add('closed');
mouth.classList.remove('remove');
},700)
setTimeout(function(){
mouth.classList.remove('closed');
element.value = '';
},800);
}
var makeChompers = () => {
//find the input elements
var inputs = document.querySelectorAll("input.chomp");
inputs.forEach((inputElement)=> {
var parent = inputElement.parentElement;
var wrapper= document.createElement('div');
wrapper.innerHTML= getTemplate(inputElement);
var newElem= wrapper.firstChild;
wrapper.remove();
inputElement.addEventListener("blur", () => {
chomp(event)
});
newElem.getElementsByClassName('top-teeth')[0].after(inputElement);
parent.appendChild(newElem);
})
}
window.onload = makeChompers;