-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (46 loc) · 1.71 KB
/
app.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
const slider = document.querySelector('#slider');
const sliderOutput = document.querySelector('#sliderOutput');
const mainGrid = document.querySelector('#grid');
// buttons
const blackBtn = document.querySelector('#blackBtn');
const rainbowBtn = document.querySelector('#rainbowBtn');
const eraserBtn = document.querySelector('#eraser');
let currentSize = 5
sliderOutput.textContent = currentSize
slider.onchange = (e) => {
let gridDivs = document.querySelectorAll('.gridDiv')
currentSize = e.target.value
sliderOutput.textContent = currentSize
gridDivs.forEach(div => {div.remove();})
mainGrid.style.cssText += `grid-template: repeat(${currentSize}, 1fr) / repeat(${currentSize}, 1fr)`;
for (let i = 0; i < (currentSize * currentSize); i++) {
let gridDiv = document.createElement('div')
gridDiv.className = 'gridDiv';
mainGrid.appendChild(gridDiv);
}
}
blackBtn.addEventListener('click', () => {
mainGrid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = 'black'
e.target.style.border = 'none'
})
})
function randomColor(){
let r = Math.floor(Math.random() * 255) + 1
let g = Math.floor(Math.random() * 255) + 1
let b = Math.floor(Math.random() * 255) + 1
let rgb = `rgb(${r},${g},${b})`
return rgb
}
rainbowBtn.addEventListener('click', () => {
mainGrid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = randomColor()
e.target.style.border = 'none'
})
})
eraserBtn.addEventListener('click', () => {
mainGrid.addEventListener('mouseover', (e) => {
e.target.style.backgroundColor = 'white'
e.target.style.border = '#edede98e solid 1px';
})
})