Grid create and remove in functions, added changing grid size
This commit is contained in:
		
							parent
							
								
									e6435e500e
								
							
						
					
					
						commit
						7c9c46779f
					
				|  | @ -10,7 +10,8 @@ | ||||||
| <body> | <body> | ||||||
|     <h1>Sketchpad</h1> |     <h1>Sketchpad</h1> | ||||||
|     <div id="container"> |     <div id="container"> | ||||||
|          | 
 | ||||||
|     </div> |     </div> | ||||||
|  |     <button id="gridsize">Change size of grid</button> | ||||||
| </body> | </body> | ||||||
| </html> | </html> | ||||||
							
								
								
									
										48
									
								
								script.js
								
								
								
								
							
							
						
						
									
										48
									
								
								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"}); | function removeGrid() { | ||||||
|     griddy.addEventListener("mouseout", (event) => { |     document.querySelectorAll(".flexbox").forEach(flexbox => flexbox.remove()); | ||||||
|         setTimeout(() => event.target.style.backgroundColor = "#fff", 1000); |     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