-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (70 loc) · 2.95 KB
/
script.js
File metadata and controls
86 lines (70 loc) · 2.95 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
85
86
const categorySelect = document.getElementById("category");
const norrifyButton = document.getElementById("norrifyButton");
const imageContainer = document.getElementById("imageContainer");
const randomImage = document.getElementById("randomImage");
const quoteText = document.getElementById("quoteText");
const quoteContainer = document.getElementById("quoteContainer");
// API URLs for the respective categories and quotes
const catAPI = "https://api.thecatapi.com/v1/images/search";
const randomImageAPI = "https://picsum.photos/500";
const chuckNorrisQuoteAPI = "https://api.chucknorris.io/jokes/random";
norrifyButton.addEventListener("click", async () => {
const category = categorySelect.value;
let imageUrl = "";
let quote = "";
try {
// fetching random image based on the selected category
if (category === "cats") {
// fetch cat image from thecatapi.com (returns an array)
const catResponse = await fetch(catAPI);
const catData = await catResponse.json();
imageUrl = catData[0].url;
} else if (category === "random") {
// fetch random image from picsum.photos with cache-busting query (important!)
imageUrl = `${randomImageAPI}?random=${Math.random()}`;
}
// fetching random Chuck Norris joke
const quoteResponse = await fetch(chuckNorrisQuoteAPI);
const quoteData = await quoteResponse.json();
quote = quoteData.value;
// set the image and quote
randomImage.src = imageUrl;
quoteText.textContent = quote;
// show the image and quote with animations
imageContainer.style.display = "block";
randomImage.style.opacity = 0;
randomImage.style.transform = "scale(0.9)";
quoteContainer.style.opacity = 0;
// apply animations for smoother transitions
setTimeout(() => {
randomImage.style.transition = "opacity 1s, transform 1s";
randomImage.style.opacity = 1;
randomImage.style.transform = "scale(1)";
quoteContainer.style.transition = "opacity 1s";
quoteContainer.style.opacity = 1;
}, 100);
// in case something goes wrong
} catch (error) {
console.error("Error fetching data:", error);
quoteText.textContent = "Sorry, something went wrong!";
}
});
// light/dark toggle
const darknessToggle = document.getElementById("darknessToggle");
const body = document.body;
const toggleText = document.querySelector(".boxOfDarkness p");
darknessToggle.addEventListener('click', () => {
// toggle dark mode class on body
body.classList.toggle('darkMode');
darknessToggle.classList.add('clicked');
// change the text depending on current light/dark mode
if (body.classList.contains('darkMode')) {
toggleText.textContent = "Click on Chuck to toggle light mode";
} else {
toggleText.textContent = "Click on Chuck to toggle dark mode";
}
// reset the animation after the button completes the movement
setTimeout(() => {
darknessToggle.classList.remove('clicked');
}, 1000); // matches animation duration in CSS (1s)
});