-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
51 lines (44 loc) · 1.6 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
const button = document.getElementById("new-advice");
const root = document.documentElement;
// Event listeners
document.addEventListener("DOMContentLoaded", getAdvice); // for first advice
button.addEventListener("click", () => {
getAdvice; // for first advice
randomizeAccentColor();
}); // for subsequent advices
button.addEventListener("click", getAdvice); // for subsequent advices
// API function
function getAdvice() {
const API = `https://api.adviceslip.com/advice?=${new Date().getTime()}`;
fetch(API)
.then((response) => {
return response.json(); // this will convert response to json
})
.then((data) => {
const advice = data.slip.advice;
const adviceId = data.slip.id;
document.getElementById("advice-text").textContent = advice;
document.querySelector("h1").textContent = "ADVICE #".concat(adviceId);
})
.catch((error) => {
console.error(error);
});
}
// Color randomization
const footer = document.querySelectorAll(".footer-link");
// Above returns an array-like object; we have to loop through it
footer.forEach((link) => {
link.addEventListener("mouseenter", randomizeLinkColor);
});
// https://bobbyhadz.com/blog/javascript-addeventlistener-is-not-a-function
// https://flaviocopes.com/how-to-add-event-listener-multiple-elements-javascript/
function randomizeLinkColor() {
root.style.setProperty("--ft-rand-color", getRandomColor());
}
function randomizeAccentColor() {
root.style.setProperty("--accent", getRandomColor());
}
function getRandomColor() {
let hue = Math.floor(Math.random() * 360);
return `hsl(${hue}, 100%, 66%)`;
}