-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (34 loc) · 1.25 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
function generateRandomHexCode() {
const randomValue = Math.floor(Math.random() * 0xffffff);
return '#' + randomValue.toString(16).padStart(6, '0');
}
const colors = document.querySelectorAll('.color');
function updateColors() {
colors.forEach(color => {
const randomColor = generateRandomHexCode();
const copyIndicator = color.closest('li').querySelector('.hex-code');
color.style.backgroundColor = randomColor;
const hexCodeElement = color.closest('li').querySelector('.hex-code');
hexCodeElement.textContent = randomColor;
color.addEventListener('click', () => {
color.classList.add('clicked');
navigator.clipboard.writeText(randomColor);
copyIndicator.textContent = "copied";
});
color.addEventListener('mouseover', () => {
copyIndicator.textContent = "copy";
});
color.addEventListener('mouseleave', () => {
copyIndicator.textContent = randomColor;
})
color.addEventListener('mouseleave', () => {
color.classList.remove('clicked');
});
});
}
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
updateColors();
}
});
updateColors();