-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (56 loc) · 2.55 KB
/
Copy pathscript.js
File metadata and controls
68 lines (56 loc) · 2.55 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
document.addEventListener('DOMContentLoaded', function () {
const circle = document.querySelector('.circle');
const diceImg = document.querySelector('.dice-img');
// Function to handle rotation and fetch random advice
const handleUserClick = async () => {
try {
// Fetch random advice from the API
const response = await fetch('https://api.adviceslip.com/advice');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
// Update the text content using the data from the API
const adviceIdElement = document.getElementById('advice-id');
const adviceTextElement = document.getElementById('advice-text');
adviceIdElement.innerText = data.slip.id;
adviceTextElement.innerText = `"${data.slip.advice}"`;
// Rotate elements to 60 degrees
circle.style.transform = 'rotate(60deg)';
diceImg.style.transform = 'rotate(60deg)';
// Delay the second rotation for 3 seconds
setTimeout(() => {
// Rotate elements back to 0 degrees
circle.style.transform = 'rotate(0deg)';
diceImg.style.transform = 'rotate(0deg)';
}, 500);
} catch (error) {
// Handle errors
console.error('There was a problem:', error);
}
};
// Function to load advice initially
const loadInitialAdvice = async () => {
try {
// Fetch advice from the API
const response = await fetch('https://api.adviceslip.com/advice');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
// Update the text content using the data from the API
const adviceIdElement = document.getElementById('advice-id');
const adviceTextElement = document.getElementById('advice-text');
adviceIdElement.innerText = data.slip.id;
adviceTextElement.innerText = `"${data.slip.advice}"`;
} catch (error) {
// Handle errors
console.error('There was a problem:', error);
}
};
// Call the function to load initial advice when the page loads
loadInitialAdvice();
// Add click event listeners to trigger rotation and fetch random advice
circle.addEventListener('click', handleUserClick);
diceImg.addEventListener('click', handleUserClick);
});