2024-01-09 19:07:08 +00:00
|
|
|
const container = document.querySelector("#container");
|
|
|
|
|
2024-01-09 19:33:07 +00:00
|
|
|
function randomizeColor() {
|
|
|
|
const hex = "0123456789abcdef";
|
|
|
|
let output = "#";
|
|
|
|
for (let i = 0; i < 6; i++) {
|
|
|
|
output += hex[Math.floor(Math.random() * 16)];
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2024-01-09 19:16:14 +00:00
|
|
|
function createGrid(size) {
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
let flexbox = document.createElement("div");
|
|
|
|
flexbox.classList.add("flexbox");
|
|
|
|
for (let j = 0; j < size; j++) {
|
|
|
|
let div = document.createElement('div');
|
|
|
|
div.classList.add("block");
|
|
|
|
flexbox.appendChild(div);
|
|
|
|
}
|
|
|
|
container.appendChild(flexbox);
|
2024-01-09 19:07:08 +00:00
|
|
|
}
|
2024-01-09 19:16:14 +00:00
|
|
|
|
|
|
|
let blocks = document.querySelectorAll(".block");
|
|
|
|
blocks.forEach(block => {
|
2024-01-09 19:33:07 +00:00
|
|
|
block.addEventListener("mouseover", (event) => {
|
|
|
|
event.target.style.backgroundColor = randomizeColor();
|
2024-01-09 19:16:14 +00:00
|
|
|
});
|
2024-01-09 19:33:07 +00:00
|
|
|
// block.addEventListener("mouseout", (event) => {
|
|
|
|
// setTimeout(() => {
|
|
|
|
// event.target.style.backgroundColor = "#fff";
|
|
|
|
// }, 1 * 1000);
|
|
|
|
// });
|
2024-01-09 19:16:14 +00:00
|
|
|
})
|
2024-01-09 19:07:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 19:16:14 +00:00
|
|
|
createGrid(16);
|
|
|
|
|
|
|
|
function removeGrid() {
|
|
|
|
document.querySelectorAll(".flexbox").forEach(flexbox => flexbox.remove());
|
|
|
|
document.querySelectorAll(".block").forEach(block => block.remove());
|
|
|
|
}
|
|
|
|
|
2024-01-09 19:33:07 +00:00
|
|
|
function clearGrid() {
|
|
|
|
document.querySelectorAll(".block").forEach(block => {
|
|
|
|
block.style.backgroundColor = "#fff";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector("button#clear").addEventListener('click', clearGrid);
|
|
|
|
|
2024-01-09 19:16:14 +00:00
|
|
|
document.querySelector("button#gridsize").addEventListener('click', () => {
|
|
|
|
let newGridSize = prompt("Provide new size of grid, provide one number, it will be processed as (VALUExVALUE - square grid):");
|
2024-01-09 19:33:07 +00:00
|
|
|
if (!isNaN(newGridSize) && newGridSize != null && newGridSize != "") {
|
2024-01-09 19:16:14 +00:00
|
|
|
removeGrid();
|
|
|
|
createGrid(newGridSize);
|
|
|
|
}
|
|
|
|
});
|