-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
59 lines (52 loc) · 2.01 KB
/
Copy pathjavascript.js
File metadata and controls
59 lines (52 loc) · 2.01 KB
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
const container = document.getElementById('container');
function getRandomRGB() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return `rgb(${r}, ${g}, ${b})`;
}
for (let i = 0; i < 256; i++) {
const square = document.createElement('div');
square.classList.add('square');
square.style.opacity = "0.1";
square.addEventListener('mouseover', () => {
square.style.backgroundColor = getRandomRGB();
let currentOpacity = parseFloat(square.style.opacity);
if (currentOpacity < 1) {
square.style.opacity = (currentOpacity + 0.1).toString();
}
});
container.appendChild(square);
}
const button = document.getElementById('reset-button');
button.addEventListener('click', () => {
const squares = document.querySelectorAll('.square');
squares.forEach(square => {
square.style.backgroundColor = '';
square.style.opacity = "0.1";
});
});
const buttonNewGrid = document.getElementById('new-grid-button');
buttonNewGrid.addEventListener('click', () => {
let userInput = prompt("Enter new grid size (max 100) :");
let gridSize = parseInt(userInput);
if (gridSize < 1 || gridSize > 100){
alert("Invalid input. Please enter a number between 1 and 100.");
return;
}
container.innerHTML = '';
for (let i = 0; i < gridSize * gridSize; i++){
const square = document.createElement('div');
square.classList.add('square');
square.style.opacity = "0.1";
square.style.flex = `1 0 calc(100% / ${gridSize})`; // Adjust size based on grid
square.addEventListener('mouseover', () => {
square.style.backgroundColor = getRandomRGB();
let currentOpacity = parseFloat(square.style.opacity);
if (currentOpacity < 1) {
square.style.opacity = (currentOpacity + 0.1).toString();
}
});
container.appendChild(square);
}
});