-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
75 lines (70 loc) · 2.04 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
const body = document.querySelector('body');
const mainContainer = document.querySelector('.container');
const button = document.createElement('button');
let pass = 10;
const mOver = function () {
if (pass < 1) {
pass = 10;
}
let b = ((pass * 10));
let div = this;
/***
* Random RGB colors on hover!
let r = Math.round(Math.random() * 150);
let g = Math.round(Math.random() * 170);
let b = Math.round(Math.random() * 190);
div.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
*/
div.style.backgroundColor = `rgb(${b}, ${b}, ${b})`;
pass--;
}
const action = function () {
input = parseInt(prompt('How many squares you want in a div?'));
Remove();
Start(input);
}
function Start(input) {
let totalSquares = (input * input);
let gridSquaresPercent = 100/(input);
let mainContainerStyle = `
min-width: 360px;
min-height: 360px;
max-width: 360px;
max-height: 360px;
display: flex;
flex-wrap: wrap;
aspect-raio: 1/1;
`;
let gridSquaresStyle = `
box-sizing: border-box;
background-color: white;
aspect-ratio: 1/1;
border: 1px solid black;
color: black;
flex: 1 1 ${gridSquaresPercent}%;
`;
let buttonStyle = `
border-radius: 6px;
padding: 5px 10px;
`;
mainContainer.setAttribute('style', mainContainerStyle);
button.textContent = "Size";
button.setAttribute('style', buttonStyle);
button.addEventListener('click', action);
for (let i = 0; i < totalSquares; i++) {
const gridDiv = document.createElement('div');
gridDiv.id = i;
gridDiv.setAttribute('style', gridSquaresStyle);
gridDiv.addEventListener('mouseover', mOver);
mainContainer.append(gridDiv);
}
}
function Remove() {
if (document.querySelectorAll('.container>div')){
document.querySelectorAll('.container>div').forEach(div => {
div.remove();
})
}
}
Start(12);
body.insertBefore(button, mainContainer);