-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
70 lines (61 loc) · 2.16 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
let squares = null
let container = document.querySelector('#grid')
let setting = 'normal';
function addSquares(dimension) {
container.innerHTML = ''
for (let row = 0; row < dimension; row++) {
for (let col = 0; col < dimension; col++) {
let newElement = document.createElement('div')
newElement.classList.add('square')
container.appendChild(newElement)
}
}
}
function setListeners() {
squares = document.querySelectorAll('.square')
squares.forEach((square) => {
square.brightness = 1;
square.addEventListener('mouseenter', () => {
switch (setting) {
case 'normal':
square.style.backgroundColor = "#735D78";
break
case 'rainbow':
var randomColor = Math.floor(Math.random() * 16777215).toString(16);
square.style.backgroundColor = "#" + randomColor;
break
case 'darker':
console.log('brightness', square.brightness)
square.brightness -= 0.1;
square.style.filter = `brightness(${square.brightness})`
break
case 'lighten':
console.log('brightness', square.brightness)
square.brightness += 0.1;
square.style.filter = `brightness(${square.brightness})`
break
}
});
});
}
let btn = document.querySelector('.btn')
let drawn = document.querySelectorAll('.drawn')
btn.addEventListener('click', () => {
let input = prompt("Select a new number of squares per side.")
if (input === null) {
return
}
squares.forEach((item) => {
item.classList.remove('drawn')
});
document.getElementById("grid").style.gridTemplateColumns = `repeat(${input}, 1fr)`;
document.getElementById("grid").style.gridTemplateRows = `repeat(${input}, 1fr)`;
addSquares(input);
setListeners();
})
function handleClick(myRadio) {
setting = myRadio.value;
// alert('New value: ' + myRadio.value);
}
addSquares(16);
setListeners();