-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (38 loc) · 1.2 KB
/
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
const toAdd = document.createDocumentFragment();
const container = document.getElementById('container');
const box = document.getElementsByClassName('box');
let boxSize = 0;
createBox(16);
function createBox(boxSize){
let ratioDim = 500 / boxSize;
container.style.visibility = 'visible';
container.style.gridTemplateColumns = `repeat(${boxSize}, ${ratioDim}px)`;
for (let i = 1; i <= (boxSize * boxSize); i++){
const newDiv = document.createElement('div');
newDiv.id = 'r'+i;
newDiv.className = 'box';
newDiv.style.height = `${ratioDim}px`
newDiv.style.width = `${ratioDim}px`
newDiv.addEventListener('mouseenter', function(){
document.getElementById('r'+i).style.background = 'black'
}, false);
toAdd.appendChild(newDiv);
}
container.appendChild(toAdd);
}
function removeBox(){
container.style.visibility = 'hidden';
while (container.firstChild) {
container.removeChild(container.lastChild);
}
}
function newBox(){
removeBox();
boxSize = prompt('Box Size');
if (boxSize > 100){
boxSize = 100;
} else if (boxSize == null){
boxSize=8;
}
createBox(boxSize);
}