Grid create and remove in functions, added changing grid size
This commit is contained in:
parent
e6435e500e
commit
7c9c46779f
|
@ -12,5 +12,6 @@
|
||||||
<div id="container">
|
<div id="container">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
<button id="gridsize">Change size of grid</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
46
script.js
46
script.js
|
@ -1,21 +1,37 @@
|
||||||
const container = document.querySelector("#container");
|
const container = document.querySelector("#container");
|
||||||
|
|
||||||
for (let i = 0; i < 16; i++) {
|
function createGrid(size) {
|
||||||
let flexbox = document.createElement("div");
|
for (let i = 0; i < size; i++) {
|
||||||
flexbox.classList.add("flexbox");
|
let flexbox = document.createElement("div");
|
||||||
for (let j = 0; j < 16; j++) {
|
flexbox.classList.add("flexbox");
|
||||||
let div = document.createElement('div');
|
for (let j = 0; j < size; j++) {
|
||||||
div.classList.add("griddy");
|
let div = document.createElement('div');
|
||||||
flexbox.appendChild(div);
|
div.classList.add("block");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in New Issue