Grid create and remove in functions, added changing grid size

This commit is contained in:
NetMan 2024-01-09 20:16:14 +01:00
parent e6435e500e
commit 7c9c46779f
3 changed files with 35 additions and 18 deletions

View File

@ -12,5 +12,6 @@
<div id="container"> <div id="container">
</div> </div>
<button id="gridsize">Change size of grid</button>
</body> </body>
</html> </html>

View File

@ -1,21 +1,37 @@
const container = document.querySelector("#container"); const container = document.querySelector("#container");
for (let i = 0; i < 16; i++) { function createGrid(size) {
for (let i = 0; i < size; i++) {
let flexbox = document.createElement("div"); let flexbox = document.createElement("div");
flexbox.classList.add("flexbox"); flexbox.classList.add("flexbox");
for (let j = 0; j < 16; j++) { for (let j = 0; j < size; j++) {
let div = document.createElement('div'); let div = document.createElement('div');
div.classList.add("griddy"); div.classList.add("block");
flexbox.appendChild(div); flexbox.appendChild(div);
} }
container.appendChild(flexbox); container.appendChild(flexbox);
}
let blocks = document.querySelectorAll(".block");
blocks.forEach(block => {
block.addEventListener("mouseover", (event) => {event.target.style.backgroundColor = "#000"});
block.addEventListener("mouseout", (event) => {
setTimeout(() => event.target.style.backgroundColor = "#fff", 1 * 1000);
});
})
} }
let griddies = document.querySelectorAll(".griddy"); createGrid(16);
griddies.forEach(griddy => {
griddy.addEventListener("mouseover", (event) => {event.target.style.backgroundColor = "#000"});
griddy.addEventListener("mouseout", (event) => {
setTimeout(() => event.target.style.backgroundColor = "#fff", 1000);
}); function removeGrid() {
}) document.querySelectorAll(".flexbox").forEach(flexbox => flexbox.remove());
document.querySelectorAll(".block").forEach(block => block.remove());
}
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):");
if (!isNaN(newGridSize)) {
removeGrid();
createGrid(newGridSize);
}
});

View File

@ -7,7 +7,7 @@
display: flex; display: flex;
} }
.griddy { .block {
border: 1px solid #000; border: 1px solid #000;
/* padding: 10px; */ /* padding: 10px; */
flex: 1; flex: 1;