-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
47 lines (37 loc) · 1.54 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
44
45
46
47
const gradientBox =document.querySelector(".gradient-box");
const inputs=document.querySelectorAll(".colors input");
const selectBox=document.querySelector(".select-box select");
const textarea=document.querySelector("textarea");
const refresh=document.querySelector(".refresh");
const copy=document.querySelector(".copy");
// Addind addEventListener on Inputs
inputs.forEach(input => {
// Event listener for changing the grasdient when input color changes
input.addEventListener("input",()=> gradient(false))
});
selectBox.addEventListener("change",()=> gradient(false))
refresh.addEventListener("click",()=>gradient(true))
copy.addEventListener("click",copyColor)
// Fo r setting Gradient value in Gradient Box and Boady
function gradient(isRandom){
if(isRandom){
inputs[0].value=randomColor()
inputs[1].value=randomColor()
}
// genrated gradient value
const gradientColor= `linear-gradient(${selectBox.value}, ${inputs[0].value}, ${inputs[1].value})`;
gradientBox.style.backgroundImage=gradientColor;
document.body.style.backgroundImage=gradientColor;
textarea.textContent=`background-image:${gradientColor}`
}
// For Genrating Random color for Refresh button
function randomColor(){
const color= Math.floor(Math.random()*0xFFFFFF).toString(16)
return `#${color}`
}
// For Coping color on the clipboard for Copy Button
function copyColor(){
navigator.clipboard.writeText(textarea.value)
copy.innerHTML=`Color Copied ✓`
setTimeout(()=>{copy.textContent="Copy Color"},2000)
}