Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
151 lines (126 loc) · 5.38 KB
/
Copy pathmain.js
File metadata and controls
151 lines (126 loc) · 5.38 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const startMenu = document.getElementById("start-menu");
const jokeContainer = document.getElementById("joke-container");
const jokeOutput = document.getElementById("joke-output");
// Start- und Übergangs-Funktionen anpassen
function startProgramm() {
// Das Startmenü wird ausgeblendet
startMenu.style.display = "none";
// Der Joke-Container wird angezeigt
jokeContainer.style.display = "block";
jokeContainer.classList.add("fadeIn"); // Animation anwenden
document.body.classList.add("color-transition");
// Den Titel "Find the perfect joke!" animieren
const jokeTitle = document.getElementById("find-joke-title");
jokeTitle.style.opacity = 1; // Sicherstellen, dass der Titel nach dem Laden sichtbar wird
}
// Die Funktion zur Suche und Anzeige des Witzes bleibt gleich, keine Änderungen nötig.
async function generateJoke() {
try {
// API-Abfrage für einen zufälligen Dad Joke
const response = await fetch('https://icanhazdadjoke.com/', {
headers: {
'Accept': 'application/json', // Wir möchten die Antwort im JSON-Format
},
});
if (!response.ok) {
throw new Error("API-Antwort ist nicht OK");
}
const data = await response.json(); // Antwort in JSON umwandeln
// Überprüfen, ob die Antwort einen Witz enthält
if (data.joke) {
// Den Witz in das Ausgabe-Element einfügen
jokeOutput.textContent = data.joke;
} else {
jokeOutput.textContent = "Oops, kein Witz gefunden!";
}
} catch (error) {
console.error("Fehler beim Abrufen des Witzes:", error);
jokeOutput.textContent = "Fehler beim Laden des Witzes.";
}
}
async function searchJoke() {
const searchTerm = document.getElementById("joke-search").value;
// Überprüfen, ob der Suchbegriff leer ist
if (searchTerm.trim() === "") {
jokeOutput.textContent = "Bitte einen Suchbegriff eingeben!";
return;
}
try {
// API-Abfrage für die Suche nach einem Witz basierend auf dem Suchbegriff
const response = await fetch(`https://icanhazdadjoke.com/search?term=${searchTerm}`, {
headers: {
'Accept': 'application/json', // Wir möchten die Antwort im JSON-Format
},
});
if (!response.ok) {
throw new Error("API-Antwort ist nicht OK");
}
const data = await response.json(); // Antwort in JSON umwandeln
// Überprüfen, ob es Ergebnisse gibt
if (data.results && data.results.length > 0) {
jokeOutput.textContent = data.results[0].joke; // Zeige den ersten Witz
} else {
jokeOutput.textContent = `Kein Witz gefunden für '${searchTerm}'`;
}
} catch (error) {
console.error("Fehler beim Abrufen des Witzes:", error);
jokeOutput.textContent = "Fehler beim Laden des Witzes.";
}
}
function copyJoke() {
const text = jokeOutput.textContent;
navigator.clipboard.writeText(text).then(() => {
showCopyNotification("Joke Copied!"); // Benachrichtigung anzeigen
});
}
function showCopyNotification(message) {
// Benachrichtigungs-Element erstellen
const notification = document.createElement("div");
notification.classList.add("copy-notification");
notification.textContent = message;
document.body.appendChild(notification);
// Animation für das Einblenden und das Verlassen der Benachrichtigung
setTimeout(() => {
notification.classList.add("show");
}, 100); // Verzögerung für das Einblenden
// Benachrichtigung nach 3 Sekunden verschwinden lassen
setTimeout(() => {
notification.classList.remove("show");
notification.classList.add("hide");
// Nach der Animation das Element entfernen
setTimeout(() => {
notification.remove();
}, 500); // Verstecken nach der Animation
}, 3000);
}
// Funktion für Partikel
function createParticles() {
const numParticles = 20; // Anzahl der Partikel
for (let i = 0; i < numParticles; i++) {
const particle = document.createElement("div");
particle.classList.add("particle");
document.body.appendChild(particle);
// Zufällige Position und Animation
const size = Math.random() * 10 + 5; // Zufällige Größe
const duration = Math.random() * 3 + 2; // Zufällige Dauer der Animation
const x = Math.random() * window.innerWidth; // Zufällige x-Position
const y = Math.random() * window.innerHeight; // Zufällige y-Position
particle.style.width = `${size}px`;
particle.style.height = `${size}px`;
particle.style.left = `${x}px`;
particle.style.top = `${y}px`;
particle.style.animationDuration = `${duration}s`;
// Entfernen der Partikel nach der Animation
setTimeout(() => {
particle.remove();
}, duration * 1000); // nach Dauer entfernen
}
}
// Aufruf der Partikel-Erstellungsfunktion
createParticles();
// Alle 2 Sekunden neue Partikel erstellen
setInterval(createParticles, 2000);
window.startProgramm = startProgramm;
window.generateJoke = generateJoke;
window.searchJoke = searchJoke;
window.copyJoke = copyJoke;