-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
104 lines (84 loc) · 2.63 KB
/
js.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const squareDivs = document.querySelector('.squareDivs');
const rainbowbtn = document.querySelector('.rainbowbtn')
const blackbtn = document.querySelector('.blackbtn')
const eraserbtn = document.querySelector('.eraserbtn')
const cleanbtn = document.querySelector('.cleanbtn')
let quantify = 0
function size() {
const size = document.getElementById("size").value;
quantify = size
}
function createDivs() {
let totalOfDivs = (quantify * quantify);
for (i = 0; i < `${totalOfDivs}`; i++) {
const divs = document.createElement('div');
divs.classList.add('gridDivs');
squareDivs.appendChild(divs)
}
squareDivs.setAttribute('style', `grid-template-columns: repeat(${quantify}, 1fr`)
}
function paintBlack() {
const divsBlack = document.querySelectorAll('.gridDivs')
divsBlack.forEach(gridDivs => {
gridDivs.addEventListener('mouseover', () => {
gridDivs.setAttribute('style', 'background-color: black')
})
})
}
function eraser() {
const divsBlack = document.querySelectorAll('.gridDivs')
divsBlack.forEach(gridDivs => {
gridDivs.addEventListener('mouseover', () => {
gridDivs.setAttribute('style', 'background-color: whitesmoke;')
})
})
}
function rainbowColor() {
const divsBlack = document.querySelectorAll('.gridDivs')
divsBlack.forEach(gridDivs => {
gridDivs.addEventListener('mouseover', () => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
gridDivs.setAttribute('style', `background-color: #${randomColor}`)
})
})
}
function clean() {
const divsBlack = document.querySelectorAll('.gridDivs')
divsBlack.forEach(gridDivs => {
gridDivs.setAttribute('style', 'background-color: whitesmoke;')
})
}
function deleteAll() {
const divsBlack = document.querySelectorAll('.gridDivs')
divsBlack.forEach(gridDivs => {
gridDivs.remove()
})
}
function selectOption() {
rainbowbtn.addEventListener('click', () => {
rainbowColor();
})
eraserbtn.addEventListener('click', () => {
eraser();
})
blackbtn.addEventListener('click', () => {
paintBlack();
})
cleanbtn.addEventListener('click', () => {
clean();
})
}
function start() {
const start = document.querySelector(".startbtn")
start.addEventListener('click', () => {
size()
if (quantify < 12 || quantify > 64) {
alert("Select a number recommended! The page will not run correctly")
} else {
deleteAll()
createDivs()
}
})
}
start();
selectOption();