-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (59 loc) · 2.28 KB
/
script.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let createdDivsContainer = document.querySelector("#createdDivsContainer");
const containerSize = 600;
let squaresPerSide = 16;
createdDivsContainer.style.width = createdDivsContainer.style.height = `${containerSize}px`;
let button1 = document.querySelector("#button1")
let button2 = document.querySelector("#button2")
function createdGrid() {
const numOfSquares = (squaresPerSide * squaresPerSide);
const widthOrHeight = `${(containerSize / squaresPerSide) - 2}px`;
for (let i = 0; i < numOfSquares; i++) {
const createdDivs = document.createElement("div");
createdDivs.style.width = createdDivs.style.height = widthOrHeight;
createdDivsContainer.appendChild(createdDivs);
}
}
createdGrid();
function divHoverEffect(event) {
if (!event.target.classList.contains("divContainerStyling")) {
event.target.classList.add("hoverColor");
}
}
createdDivsContainer.addEventListener('mouseover', (event) => {
divHoverEffect(event);
});
//placeholder event listener for mobile friendly interaction.
// createdDivsContainer.addEventListener("", (event) => {
// divHoverEffect(event);
// });
button1.addEventListener('click', (event) => {
clearDivHoverEffect(event);
});
button2.addEventListener('click', (event) => {
userCreatedGrid(event);
console.log(event);
});
function clearDivHoverEffect() {
let removeDivClass = document.querySelectorAll("div");
removeDivClass.forEach(div => {
div.classList.remove("hoverColor");
})
};
function userCreatedGrid() {
squaresPerSide = prompt("Enter the number for your grid size");
//makes sure users cant put value higher than 100 for performance reasons.
if (squaresPerSide > 100) {
squaresPerSide = 16;
}
const newNumberOfSquares = (squaresPerSide * squaresPerSide);
const widthOrHeight = `${(containerSize / squaresPerSide) - 2}px`;
while (createdDivsContainer.firstChild) {
createdDivsContainer.removeChild(createdDivsContainer.firstChild);
}
for (let i = 0; i < newNumberOfSquares; i++) {
let userDivs = document.createElement("div");
userDivs.style.width = userDivs.style.height = widthOrHeight;
createdDivsContainer.appendChild(userDivs);
userDivs.addEventListener("mouseover", divHoverEffect);
}
}