-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (49 loc) · 1.46 KB
/
Copy pathapp.js
File metadata and controls
60 lines (49 loc) · 1.46 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
59
60
let trailEnabled = false;
function createGrid() {
const gridSize = document.getElementById('grid-size').value;
const gridContainer = document.getElementById('grid-container');
// Clear previous grid
gridContainer.innerHTML = '';
// Set custom property for grid size
document.documentElement.style.setProperty('--grid-size', gridSize);
// Generate a random color for hover effect
const hoverColor = getRandomColor();
// Create the grid
for (let i = 0; i < gridSize * gridSize; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
gridContainer.appendChild(cell);
}
// Enable trail on mouse leave
gridContainer.addEventListener('mouseleave', () => {
trailEnabled = true;
});
// Disable trail on mouse enter
gridContainer.addEventListener('mouseenter', () => {
trailEnabled = false;
});
// Change color on hover
const cells = document.querySelectorAll('.cell');
cells.forEach(cell => {
cell.addEventListener('mouseenter', () => {
if (!trailEnabled) {
cell.style.backgroundColor = hoverColor;
}
});
cell.addEventListener('mouseleave', () => {
if (!trailEnabled) {
cell.style.backgroundColor = 'color';
}
});
});
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
// Initial grid creation
createGrid();