-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
84 lines (74 loc) · 1.93 KB
/
Copy pathpopup.js
File metadata and controls
84 lines (74 loc) · 1.93 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
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
const input1 = document.getElementById('input1')
const input2 = document.getElementById('input2')
const randomBtn = document.querySelector('.random')
const body = document.querySelector('.container')
const message = document.getElementById('message')
const hexcodes = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
]
// EVENTS
input1.addEventListener('input', function () {
let firstColor = input1.value
body.style.background = `linear-gradient(90deg, ${firstColor}, ${input2.value})`
updateColorMessage()
})
input2.addEventListener('input', function () {
let secondColor = input2.value
body.style.background = `linear-gradient(90deg, ${input1.value}, ${secondColor})`
updateColorMessage()
})
randomBtn.addEventListener('click', function () {
applyRandomColor()
})
// CUSTOM FUNCTION
function updateColorMessage() {
message.innerText = `background : linear-gradient(90deg, ${input1.value}, ${input2.value})`
}
function getRandomColor() {
let randomColor = '#'
while (randomColor.length < 7) {
randomColor =
randomColor + hexcodes[Math.floor(Math.random() * hexcodes.length)]
}
return randomColor
}
function applyRandomColor() {
let randomColor = ''
randomColor = getRandomColor()
let firstRandom = getRandomColor()
let secondRandom = getRandomColor()
body.style.background = `linear-gradient(90deg, ${firstRandom}, ${secondRandom})`
console.log(firstRandom, secondRandom)
input1.value = firstRandom
input2.value = secondRandom
updateColorMessage()
}
// click to #message copy to clipboard
message.addEventListener('click', function () {
// click to #message copy to clipboard
let text = message.innerText
let textArea = document.createElement('textarea')
textArea.value = text
document.body.appendChild(textArea)
textArea.select()
document.execCommand('copy')
textArea.remove()
})
// RUN AT LOAD
updateColorMessage()
applyRandomColor()