-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscript.js
33 lines (30 loc) · 1.15 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
document.addEventListener('DOMContentLoaded', () => {
const gradient = document.getElementById('gradient');
const color1 = document.querySelector('.color1');
const color2 = document.querySelector('.color2');
const cssBackground = document.getElementById('css-background');
const copyBtn = document.getElementById('copy-btn');
const updateBackground = () => {
const color1Value = color1.value;
const color2Value = color2.value;
const background = `linear-gradient(to right, ${color1Value}, ${color2Value})`;
gradient.style.background = background;
cssBackground.textContent = `background: ${background};`;
};
const copyToClipboard = () => {
const textToCopy = cssBackground.textContent;
navigator.clipboard.writeText(textToCopy).then(
() => {
alert('CSS background value copied to clipboard!');
},
(err) => {
console.error('Failed to copy: ', err);
},
);
};
color1.addEventListener('input', updateBackground);
color2.addEventListener('input', updateBackground);
copyBtn.addEventListener('click', copyToClipboard);
// Initialize background on page load
updateBackground();
});