-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
104 lines (85 loc) · 2.43 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// defining variables
var h3 = document.querySelector("h3");
var color1 = document.querySelectorAll("input")[0];
var color2 = document.querySelectorAll("input")[1];
// var body = document.getElementById("gradient");
var body = document.querySelector("body");
var direction = "right";
var button = document.querySelector(".direction");
var right = document.getElementById("first");
var left = document.getElementById("last");
var randomBtn = document.querySelector(".button");
var backgroundColor =
"linear-gradient(to "
+ direction
+ ", "
+ color1.value
+ ", "
+ color2.value
+ ")";
var randomHex = "#ffffff";
h3.textContent = "background: " + backgroundColor +";";
// function declarations
function changeBackground() {
var backgroundColor =
"linear-gradient(to "
+ direction
+ ", "
+ color1.value
+ ", "
+ color2.value
+ ")";
body.style.background = backgroundColor;
h3.textContent = "background: " + backgroundColor + ";";
}
function swapDirection() {
console.log("clicked");
if (direction === "right") {
direction = "left";
} else {
direction = "right";
}
right.classList.toggle("swap");
left.classList.toggle("swap");
changeBackground();
}
// randomize function copied from: https://www.paulirish.com/2009/random-hex-color-code-snippets/
function colorPicker() {
return '#'+Math.floor(Math.random()*16777216).toString(16);
}
function randomize() {
color1.value = colorPicker();
color2.value = colorPicker();
changeBackground();
}
// copyCSS function copied from: https://alligator.io/js/copying-to-clipboard/
function copyCSS() {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(h3);
selection.removeAllRanges();
selection.addRange(range);
try {
document.execCommand('copy');
selection.removeAllRanges();
const original = h3.textContent;
h3.textContent = 'Copied!';
h3.classList.add('success');
setTimeout(() => {
h3.textContent = original;
h3.classList.remove('success');
}, 1200);
} catch(e) {
const errorMsg = document.querySelector('.error-msg');
errorMsg.classList.add('show');
setTimeout(() => {
errorMsg.classList.remove('show');
}, 1200);
}
}
//event listeners
color1.addEventListener("input", changeBackground)
color2.addEventListener("input", changeBackground)
button.addEventListener("click", swapDirection)
randomBtn.addEventListener("click", randomize)
h3.addEventListener("click", copyCSS)