This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eas-script.js
96 lines (86 loc) · 3.32 KB
/
eas-script.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
function createDivGrid(x = 16, y = 16) {
const containerDiv = document.querySelector('#container');
for (let i = 0; i < x; i++) {
const row = document.createElement('div');
row.classList.add('row');
for (let j = 0; j < y; j++) {
const newDiv = document.createElement('div');
const divWidth = (960/x - 2) + 'px';
const divHeight = (960/x - 1) + 'px';
newDiv.style.minHeight = divHeight;
newDiv.style.minWidth = divWidth;
newDiv.classList.add('hoverDiv');
row.appendChild(newDiv);
}
containerDiv.appendChild(row);
}
}
function blackGrid() {
if (this.classList.contains('hovered9')) {
return;
} else if (this.classList.contains('hovered8')) {
this.classList.remove('hovered8');
this.classList.add('hovered9');
} else if (this.classList.contains('hovered7')) {
this.classList.remove('hovered7');
this.classList.add('hovered8');
} else if (this.classList.contains('hovered6')) {
this.classList.remove('hovered6');
this.classList.add('hovered7');
} else if (this.classList.contains('hovered5')) {
this.classList.remove('hovered5');
this.classList.add('hovered6');
} else if (this.classList.contains('hovered4')) {
this.classList.remove('hovered4');
this.classList.add('hovered5');
} else if (this.classList.contains('hovered3')) {
this.classList.remove('hovered3');
this.classList.add('hovered4');
} else if (this.classList.contains('hovered2')) {
this.classList.remove('hovered2');
this.classList.add('hovered3');
} else if (this.classList.contains('hovered1')) {
this.classList.remove('hovered1');
this.classList.add('hovered2');
} else if (this.classList.contains('hovered')) {
this.classList.remove('hovered');
this.classList.add('hovered1');
} else {this.classList.add('hovered');}
}
function colorGrid() {
const randomColor = Math.floor(Math.random()*16777215).toString(16);
this.style.backgroundColor = "#" + randomColor;
}
function clearGrid() {
const dimension = Number(prompt('Enter grid side dimension: ', '16'));
const grid = document.querySelectorAll('.row');
grid.forEach(gridRow => gridRow.remove());
createDivGrid(dimension, dimension);
listen();
}
function listen() {
const divs = document.querySelectorAll('.hoverDiv');
const bnwBtn = document.querySelector('.bnw');
const colorBtn = document.querySelector('.goColor');
if (bnwBtn.classList.contains('selected')) {
divs.forEach(div => div.addEventListener('mouseenter', blackGrid));
} else if (colorBtn.classList.contains('selected')) {
divs.forEach(div => div.addEventListener('mouseenter', colorGrid));
}
}
createDivGrid();
listen();
const clearBtn = document.querySelector('.clear');
clearBtn.addEventListener('click', clearGrid);
const colorBtn = document.querySelector('.goColor');
const bnwBtn = document.querySelector('.bnw');
colorBtn.addEventListener("click", () => {
colorBtn.classList.add('selected');
bnwBtn.classList.remove('selected');
clearGrid();
});
bnwBtn.addEventListener("click", () => {
bnwBtn.classList.add('selected');
colorBtn.classList.remove('selected');
clearGrid();
});