forked from ghostmkg/web-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
36 lines (28 loc) · 1.11 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
//DOM Elements
const color1Elem = document.getElementById('color-1');
const color2Elem = document.getElementById('color-2');
const codeElem = document.querySelector('.code');
const btn = document.querySelector('.btn');
const infoElem = document.querySelector('.info');
//Generate HEX
function getHEX(){
let randomHex = Math.random().toString(16).substr(-6);
let hexColor = `#${randomHex}`;
return hexColor;
}
//Update the UI
function updateUI(){
infoElem.remove();
color1Elem.innerText = getHEX();
color2Elem.innerText = getHEX();
color1Elem.style.background = color1Elem.innerText;
color2Elem.style.background = color2Elem.innerText;
codeElem.innerText = `background: linear-gradient(90deg, ${color1Elem.innerText}, ${color2Elem.innerText});`;
document.body.style.background = `linear-gradient(90deg, ${color1Elem.innerText}, ${color2Elem.innerText})`;
}
//when user clicks SPACE or the arrow, then UpdateUI
window.addEventListener('keydown', event => {
if(event.code === 'Space')
updateUI();
});
btn.addEventListener('click', updateUI);