-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (57 loc) · 1.78 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
let container = document.querySelector(".gridContainer");
let gridLayoutBtn = document.querySelector("#gridLayoutBtn");
let numOfRows = 16;
let numOfColumns = 16;
createGridLayout(numOfRows, numOfColumns);
function createGridLayout(numOfRows, numOfColumns) {
//Grid
//# of rows
for (let i = 0; i < numOfRows; i++) {
let row = document.createElement("div");
row.classList.add("row");
container.appendChild(row);
//# of columns
for (let j = 0; j < numOfColumns; j++) {
let square = document.createElement("div");
square.classList.add("square");
row.appendChild(square);
//hover effect
square.addEventListener('mouseover', getRandomColor);
}
}
}
gridLayoutBtn.addEventListener("click", newGridLayout);
function newGridLayout() {
deleteGrid();
numOfRows = getAndLimitRows() || 16;
numOfColumns = getAndLimitColumns() || 16;
createGridLayout(numOfRows, numOfColumns);
}
function deleteGrid() {
let rows = document.querySelectorAll(".row")
rows.forEach(row => { row.remove() });
}
function getAndLimitRows() {
let numOfRows = prompt("Rows");
while (numOfRows > 64) {
numOfRows = prompt("Rows should be up to 64");
}
return numOfRows
}
function getAndLimitColumns() {
let numOfColumns = prompt("Columns");
while (numOfColumns > 64) {
numOfColumns = prompt("Columns should be up to 64")
}
return numOfColumns
}
function randomRGBNum() {
return Math.floor((Math.random() * 255) + 1)
}
function getRandomColor(e) {
let r = randomRGBNum(), g = randomRGBNum(), b = randomRGBNum();
e.target.style.backgroundColor = `rgb(${r}, ${g}, ${b}`;
setTimeout(() => {
e.target.style.backgroundColor = "";
}, 500);
}