-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (50 loc) · 1.66 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
52
53
54
55
56
57
58
document.addEventListener('DOMContentLoaded', () => {
const startButton = document.getElementById('start-button');
const intro = document.getElementById('intro');
const poemContainer = document.getElementById('poem-container');
const title = document.getElementById('title');
const author = document.getElementById('author');
const poem = document.getElementById('poem');
const anotherPoemButton = document.getElementById('another-poem-button');
startButton.addEventListener('click', async () => {
intro.classList.add('hidden');
poemContainer.classList.remove('hidden');
await loadPoem();
});
anotherPoemButton.addEventListener('click', async () => {
await fadeOutPoem();
await loadPoem();
});
async function loadPoem() {
const response = await fetch('https://poetrydb.org/random');
const data = await response.json();
const poemData = data[0];
title.textContent = poemData.title;
author.textContent = `- ${poemData.author}`;
poem.innerHTML = '';
for (const line of poemData.lines) {
const lineElement = document.createElement('p');
lineElement.textContent = line;
poem.appendChild(lineElement);
}
await fadeInPoem();
}
function fadeOutPoem() {
return new Promise((resolve) => {
poemContainer.classList.add('hidden');
setTimeout(() => {
poemContainer.classList.remove('hidden');
resolve();
}, 1000);
});
}
function fadeInPoem() {
return new Promise((resolve) => {
poemContainer.classList.add('expanded');
setTimeout(() => {
anotherPoemButton.classList.remove('hidden');
resolve();
}, 1000);
});
}
});