-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (40 loc) · 1.75 KB
/
script.js
File metadata and controls
53 lines (40 loc) · 1.75 KB
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
// let css = document.querySelector("h3");
let css = document.getElementById('gradientTxt');
let color1 = document.querySelector(".color1");
let color2 = document.querySelector(".color2");
let body = document.getElementById("gradient");
let randomBtn = document.getElementById("random")
//sets the color for each <input type="color"> & RandomBtn background
function setGradient() {
let xyGradient = `linear-gradient(to right, ${color1.value}, ${color2.value})`;
body.style.background = xyGradient;
randomBtn.style.background = xyGradient;
css.style.background = xyGradient;
css.textContent = `linear-gradient(to right, ${color1.value}, ${color2.value});`.toUpperCase();
}
// calls setGradient function on page load
window.onload = setGradient();
// function for random values
// (1<<24) === 16777216 is using binary to translane into hex somehow
// If you want you can look up how to convert from hex to decimal. It Should be easier than
//decimal to hex because you just multiply each digit by its power of 16 for that position and
//add them up
function getRandomGradient() {
let randomColor1 = '#'+ Math.floor((Math.random()*(1<<24)|0)).toString(16);
let randomColor2 = '#'+Math.floor((Math.random()*(1<<24)|0)).toString(16);
color1.value = randomColor1;
color2.value = randomColor2;
setGradient();
css.textContent = `linear-gradient(to right, ${color1.value}, ${color2.value});`.toUpperCase();
}
// COPY txt-- BUTTOn
function copyTxt() {
let copyText = css;
copyText.select();
document.execCommand('copy');
}
document.querySelector("#copyBtn").addEventListener("click", copyTxt);
//end of copy txt code
color1.addEventListener("input", setGradient);
color2.addEventListener("input", setGradient);
randomBtn.addEventListener("click", getRandomGradient);