mirror of
https://github.com/zyqunix/tools.git
synced 2025-07-06 06:20:30 +02:00
added a random hex color generator
This commit is contained in:
parent
c83218d182
commit
50ff6ad167
4 changed files with 145 additions and 0 deletions
40
color/index.js
Normal file
40
color/index.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const colorButton = document.getElementById('colorButton');
|
||||
const fullColor = document.getElementById('fullColor');
|
||||
const main = document.getElementById('main');
|
||||
const offscreenAlert = document.getElementById('offscreenalert');
|
||||
|
||||
const chars = "0123456789ABCDEF";
|
||||
|
||||
function generateRandomHexColor() {
|
||||
return `#${Array.from({ length: 6 }, () => chars[Math.floor(Math.random() * chars.length)]).join('')}`;
|
||||
}
|
||||
|
||||
function applyColor(color) {
|
||||
fullColor.textContent = color;
|
||||
main.style.backgroundColor = color;
|
||||
}
|
||||
|
||||
function copyToClipboard(text) {
|
||||
return navigator.clipboard.writeText(text).then(() => {
|
||||
console.log("Copied to clipboard");
|
||||
}).catch(err => {
|
||||
console.error("Failed to copy content:", err);
|
||||
});
|
||||
}
|
||||
|
||||
function showAlert() {
|
||||
offscreenAlert.style.top = '10px';
|
||||
setTimeout(() => {
|
||||
offscreenAlert.style.top = '-70px';
|
||||
}, 2500);
|
||||
}
|
||||
|
||||
colorButton.addEventListener('click', () => {
|
||||
const newColor = generateRandomHexColor();
|
||||
applyColor(newColor);
|
||||
});
|
||||
|
||||
main.addEventListener('click', () => {
|
||||
const colorText = fullColor.textContent || fullColor.innerHTML;
|
||||
copyToClipboard(colorText).then(showAlert);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue