-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (48 loc) · 1.35 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
48
49
50
51
52
53
54
55
56
const grid = document.querySelector(".gridContainer");
const userInput = document.getElementById("quantity");
const resetButton = document.querySelector(".reset");
const createGrid = (size) => {
for (let i = 0; i < size * size; i++) {
const div = document.createElement("div");
div.classList.add("square");
grid.appendChild(div);
}
};
const updateGrid = () => {
grid.innerHTML = "";
grid.style.setProperty(
"grid-template-columns",
`repeat(${userInput.value}, 2fr)`
);
grid.style.setProperty(
"grid-template-rows",
`repeat(${userInput.value}, 2fr)`
);
createGrid(userInput.value);
};
const changeSquareColor = (e) => {
if (e.buttons === 1) {
e.target.style.backgroundColor = "black";
} else if (e.buttons === 2) {
e.target.style.backgroundColor = "red";
}
};
userInput.addEventListener("change", updateGrid);
resetButton.addEventListener("click", function () {
grid.innerHTML = "";
userInput.value = "";
grid.style.setProperty("grid-template-columns", `repeat(16, 2fr)`);
grid.style.setProperty("grid-template-rows", `repeat(16, 2fr)`);
createGrid(16);
});
createGrid(16);
grid.addEventListener("mousedown", (e) => {
e.preventDefault();
changeSquareColor(e);
});
grid.addEventListener("mouseover", (e) => {
e.preventDefault();
if (e.buttons === 1 || e.buttons === 2) {
changeSquareColor(e);
}
});