-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (40 loc) · 1.19 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
44
45
46
47
const root = document.querySelector(':root');
const wrapper = document.querySelector('.wrapper');
const button = document.querySelector('.container button');
const range = document.getElementById('range');
const readOut = document.querySelector('.readOut');
const currentBoxes = document.querySelectorAll('.box');
let sides = 8;
createBoxes(sides);
range.addEventListener('input', (e) => {
sides = e.target.value;
readOut.textContent = `Generate ${sides} squares per side`;
});
button.addEventListener('click', () => {
resetCurrentBoxes();
createBoxes(sides);
});
wrapper.addEventListener('mouseover', (e) => {
if(e.target.className === 'box') {
e.target.style.backgroundColor = '#877087';
}
})
function createBoxes(n) {
//Calculate the amount of boxes
const boxes = n ** 2;
//Create 16 squares within the container div
for (let i = 0; i < boxes ; i++) {
const div = document.createElement('div');
div.classList.add('box');
wrapper.appendChild(div);
};
setGridRepeat(n);
};
function setGridRepeat(n) {
root.style.setProperty('--grid-repeat', n)
};
function resetCurrentBoxes() {
currentBoxes.forEach(currentBox => {
wrapper.removeChild(currentBox);
});
};