-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
65 lines (53 loc) · 1.73 KB
/
main.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
window.onload = function () {
addColor();
};
// BUTTON
for (let i = 1; i <= 25; i++) {
const box = document.createElement("div");
box.classList.add("box");
document.querySelector(".container").appendChild(box);
}
const btn = document.querySelector(".btn");
const randomColorBlock = document.querySelectorAll(".box");
function RandomHexColorCode() {
var chars = "0123456789abcdef";
var colorlength = 6;
var color = "";
for (var i = 0; i < colorlength; i++) {
var randomColor = Math.floor(Math.random() * chars.length);
color += chars.substring(randomColor, randomColor + 1);
}
return "#" + color;
}
function addColor() {
randomColorBlock.forEach((event) => {
var newColor = RandomHexColorCode();
event.style.backgroundColor = newColor;
event.innerHTML = newColor;
// Get the element you want to add hover styles to
// Add a 'mouseenter' event listener to add the 'hovered' class when the mouse enters
event.addEventListener('mouseenter', () => {
// console.log('hovered');
event.innerHTML="copy";
});
// Add a 'mouseleave' event listener to remove the 'hovered' class when the mouse leaves
event.addEventListener('mouseleave', () => {
// console.log('nothovered');
event.innerHTML=newColor;
});
event.addEventListener('click', () => {
navigator.clipboard.writeText(newColor)
showCopyPopup(newColor);
});
});
}
//function to show copy colorcode popup
function showCopyPopup(color) {
const copyPopup = document.getElementById('copy-popup');
const copiedColor = document.getElementById('copied-color');
copiedColor.textContent = color;
copyPopup.classList.add('show');
setTimeout(() => {
copyPopup.classList.remove('show');
}, 2000);
}