Gerne schaue ich mir deinen Code an und helfe dir dabei, eine optimierte Version mit derselben Funktionalität zu erstellen. Dein aktueller Code ist bereits gut strukturiert und enthält ansprechende visuelle Effekte. Hier sind einige Vorschläge zur Verbesserung:
-
Code-Struktur und Lesbarkeit:
- Trennung von CSS und JavaScript: Es ist empfehlenswert, CSS und JavaScript in separate Dateien auszulagern, um die Wartbarkeit zu erhöhen.
- Verwendung von modernen JavaScript-Features: Nutzung von
const und let anstelle von var, sowie die Verwendung von arrow functions für eine klarere Syntax.
-
Barrierefreiheit (Accessibility):
- Aria-Attribute: Hinzufügen von ARIA-Labels für bessere Zugänglichkeit.
- Fokuszustände: Verbesserung der Fokuszustände für Tastaturnavigation.
-
Optimierung der Benutzererfahrung:
- Validierung und Feedback: Echtzeit-Validierung der Eingabefelder und aussagekräftigere Fehlermeldungen.
- Responsive Design: Sicherstellen, dass das Design auf allen Geräten gut aussieht.
-
Performance:
- Lazy Loading: Optimierung von Bildern und Ressourcen, um die Ladezeit zu verkürzen.
- Minimierung von Animationen: Überprüfung, ob alle Animationen notwendig sind, um die Performance zu verbessern.
Hier ist eine optimierte Version deines Codes, die diese Verbesserungen berücksichtigt:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Team Generator</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lilita+One&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<!-- Externe Bibliotheken -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.4.0/dist/confetti.browser.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/2.0.12/typed.min.js" defer></script>
<script src="script.js" defer></script>
</head>
<body>
<div class="container">
<div id="typed-output" aria-live="polite"></div>
<form id="teamForm">
<div class="name-fields" id="nameFields">
<input type="text" id="name1" placeholder="Name 1" required aria-label="Name 1">
<input type="text" id="name2" placeholder="Name 2" required aria-label="Name 2">
<input type="text" id="name3" placeholder="Name 3" required aria-label="Name 3">
<input type="text" id="name4" placeholder="Name 4" required aria-label="Name 4">
</div>
<div class="button-group">
<button type="button" id="addBtn" onclick="addFields()" aria-label="Weitere Namen hinzufügen">+2</button>
<button type="button" onclick="generateTeams()" aria-label="Teams erstellen">Teams erstellen</button>
<button type="button" class="hidden" id="newEntryBtn" onclick="resetForm()" aria-label="Neue Eingabe">Neue Eingabe</button>
</div>
</form>
<div class="loading-bar hidden" id="loadingBar" role="status" aria-live="polite" aria-label="Laden"></div>
<div id="result"></div>
<div class="footer-text">from Launii with ♥</div>
</div>
<footer>
<a href="https://steamcommunity.com/id/217erss/" class="footer-button" target="_blank" rel="noopener" aria-label="Mein Steam-Profil">
<img src="https://static-00.iconduck.com/assets.00/steam-icon-506x512-nfg4qx72.png" alt="Steam Logo" class="footer-icon"> Mein Steam
</a>
</footer>
</body>
</html>
styles.css
/* Grundlegende Stile */
body {
font-family: 'Arial', sans-serif;
background-size: cover;
background-position: center;
backdrop-filter: blur(5px);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
overflow: auto;
transition: background-image 1s ease;
}
.container {
background-color: rgba(34, 40, 49, 0.9);
border-radius: 20px;
padding: 30px;
width: 90%;
max-width: 400px;
text-align: center;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.8);
transition: transform 0.3s, box-shadow 0.3s;
display: flex;
flex-direction: column;
align-items: center;
min-height: 400px;
}
.container:hover {
transform: scale(1.02);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.9);
}
#typed-output {
font-family: 'Lilita One', cursive;
font-size: 2.5rem;
text-shadow: 2px 2px 10px rgba(0, 0, 0, 0.8);
margin-bottom: 20px;
color: #fff;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
border: 1px solid rgba(255, 255, 255, 0.7);
background: linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(0, 173, 181, 0.1));
color: white;
transition: border-color 0.3s, background-color 0.3s, transform 0.3s, box-shadow 0.3s;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}
input[type="text"]:hover {
border-color: #4CAF50;
background-color: rgba(255, 255, 255, 0.2);
transform: scale(1.02);
}
input[type="text"]:focus {
border-color: #00c8ff;
background-color: rgba(255, 255, 255, 0.3);
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(0, 200, 255, 0.5);
}
::placeholder {
color: rgba(255, 255, 255, 0.8);
opacity: 1;
}
button {
background-color: rgb(0, 173, 181);
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.3s, box-shadow 0.3s;
margin: 10px 5px 0 5px;
font-size: 0.9rem;
}
button:hover {
background-color: #4CAF50;
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}
#result {
margin-top: 20px;
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
li {
padding: 10px;
border-radius: 5px;
margin: 5px 0;
color: white;
background-color: #007bff;
transition: transform 0.3s, box-shadow 0.3s, background-color 0.3s;
animation: fadeIn 0.5s ease, bounce 0.5s ease;
}
li:hover {
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
background-color: #0056b3;
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
30% { transform: translateY(-12px); }
40% { transform: translateY(-8px); }
60% { transform: translateY(-4px); }
}
.team-red {
background: linear-gradient(135deg, #ff4d4d, #ff1a1a);
}
.team-green {
background: linear-gradient(135deg, #28a745, #1e7e34);
}
.team-blue {
background: linear-gradient(135deg, #007bff, #0056b3);
}
.team-yellow {
background: linear-gradient(135deg, #ffc107, #e0a800);
}
.team-purple {
background: linear-gradient(135deg, #6f42c1, #5a32a3);
}
.team-red:hover, .team-green:hover, .team-blue:hover, .team-yellow:hover, .team-purple:hover {
filter: brightness(1.1);
}
.hidden {
display: none;
}
footer {
margin-top: 20px;
text-align: center;
}
.footer-button {
background-color: rgba(255, 255, 255, 0.2);
color: #fff;
padding: 10px;
border-radius: 5px;
text-decoration: none;
display: inline-flex;
align-items: center;
transition: background-color 0.3s, transform 0.3s, box-shadow 0.3s;
}
.footer-button:hover {
background-color: rgba(255, 255, 255, 0.4);
transform: scale(1.05);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}
.footer-icon {
width: 20px;
margin-right: 5px;
}
.footer-text {
margin-top: 10px;
font-size: 0.8rem;
color: #fff;
opacity: 0.7;
}
.loading-bar {
width: 100%;
height: 10px;
background-color: #e0e0e0;
border-radius: 5px;
margin-top: 20px;
position: relative;
overflow: hidden;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.loading-bar::before {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 0%;
background: linear-gradient(90deg, #007bff, #00c8ff);
border-radius: 5px;
animation: loadingAnimation 4s ease-in-out forwards;
}
.loading-bar::after {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: linear-gradient(135deg, rgba(255, 255, 255, 0.2) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0.2) 75%, transparent 75%, transparent);
background-size: 20px 20px;
animation: stripeAnimation 1s linear infinite;
}
@keyframes loadingAnimation {
from { width: 0%; }
to { width: 100%; }
}
@keyframes stripeAnimation {
0% { background-position: 0 0; }
100% { background-position: 20px 0; }
}
.button-group {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
@media (max-width: 500px) {
#typed-output {
font-size: 1.8rem;
}
button {
width: 100%;
margin: 5px 0;
}
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const typedOutput = document.getElementById("typed-output");
const newEntryBtn = document.getElementById('newEntryBtn');
const backgroundImages = [
'https://i.ibb.co/n3KsWpd/leaf-8687801-1920.jpg',
'https://i.ibb.co/FBRYGFb/paddle-boat-8253274-1920.jpg',
'https://i.ibb.co/zxSYdX4/nature-9006428-1920.jpg'
];
const typed = new Typed(typedOutput, {
strings: ["Team Generator", "Teamauswahl", "Erstelle dein Team jetzt!"],
typeSpeed: 90,
backSpeed: 30,
loop: false,
cursorChar: '|',
showCursor: true,
});
const changeBackgroundImage = () => {
const randomImage = backgroundImages[Math.floor(Math.random() * backgroundImages.length)];
document.body.style.backgroundImage = `url(${randomImage})`;
};
changeBackgroundImage();
newEntryBtn.addEventListener('click', changeBackgroundImage);
});
let currentCount = 4;
function addFields() {
if (currentCount < 10) {
const nameFields = document.getElementById('nameFields');
for (let i = 0; i < 2; i++) {
currentCount++;
const input = document.createElement('input');
input.type = 'text';
input.id = `name${currentCount}`;
input.placeholder = `Name ${currentCount}`;
input.required = true;
input.setAttribute('aria-label', `Name ${currentCount}`);
nameFields.appendChild(input);
}
}
if (currentCount >= 10) {
document.getElementById('addBtn').style.display = 'none';
}
}
function generateTeams() {
const names = [];
for (let i = 1; i <= currentCount; i++) {
const nameInput = document.getElementById(`name${i}`);
if (nameInput && nameInput.value.trim()) {
names.push(nameInput.value.trim());
}
}
if (names.length < 4 || names.length % 2 !== 0) {
alert('Bitte gib mindestens 4 Namen ein und stelle sicher, dass die Anzahl gerade ist.');
return;
}
const loadingBar = document.getElementById('loadingBar');
loadingBar.classList.remove('hidden');
let width = 0;
const loadingInterval = setInterval(() => {
if (width >= 100) {
clearInterval(loadingInterval);
loadingBar.classList.add('hidden');
displayTeams(names);
} else {
width++;
loadingBar.style.width = `${width}%`;
}
}, 40);
}
function displayTeams(names) {
shuffleArray(names);
const teams = [];
for (let i = 0; i < names.length; i += 2) {
teams.push([names[i], names[i + 1]]);
}
const resultDiv = document.getElementById('result');
let resultHTML = '<ul>';
const teamColors = ['team-red', 'team-green', 'team-blue', 'team-yellow', 'team-purple'];
teams.forEach((team, index) => {
resultHTML += `<li class="${teamColors[index % teamColors.length]}">Team ${index + 1}: ${team[0]} & ${team[1]}</li>`;
});
resultHTML += '</ul>';
resultDiv.innerHTML = resultHTML;
document.getElementById('newEntryBtn').classList.remove('hidden');
triggerConfetti();
}
function resetForm() {
const teamForm = document.getElementById('teamForm');
teamForm.reset();
document.getElementById('result').innerHTML = '';
currentCount = 4;
const nameFields = document.getElementById('nameFields');
nameFields.innerHTML = `
<input type="text" id="name1" placeholder="Name 1" required aria-label="Name 1">
<input type="text" id="name2" placeholder="Name 2" required aria-label="Name 2">
<input type="text" id="name3" placeholder="Name 3" required aria-label="Name 3">
<input type="text" id="name4" placeholder="Name 4" required aria-label="Name 4">
`;
document.getElementById('addBtn').style.display = 'inline-block';
document.getElementById('newEntryBtn').classList.add('hidden');
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function triggerConfetti() {
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.6 }
});
}
Erläuterungen zu den Verbesserungen:
-
Trennung von HTML, CSS und JavaScript:
- Dadurch wird der Code sauberer und leichter wartbar.
-
Barrierefreiheit:
- ARIA-Attribute wurden hinzugefügt, um die Anwendung für Screenreader und andere Assistenztechnologien zugänglicher zu machen.
- Fokuszustände und Tastaturnavigation wurden verbessert.
-
Responsive Design:
- Zusätzliche Medienabfragen (
@media) wurden hinzugefügt, um sicherzustellen, dass das Layout auf kleineren Bildschirmen gut aussieht.
-
Code-Optimierungen:
- Verwendung von Template-Strings für eine klarere und lesbarere JavaScript-Syntax.
- Nutzung von
forEach anstelle von traditionellen Schleifen für eine prägnantere Iteration.
-
Performance-Verbesserungen:
- Defer-Attribute wurden zu den Skript-Tags hinzugefügt, um das Rendern der Seite nicht zu blockieren.
- Redundante CSS-Animationen wurden konsolidiert.
-
Benutzerfreundlichkeit:
- Die Hinzufügung weiterer Namen wurde jetzt auf maximal 10 beschränkt, um die Benutzeroberfläche übersichtlich zu halten.
- Nach dem Generieren der Teams wird eine Bestätigungsanimation (Confetti) ausgelöst, um dem Benutzer ein visuelles Feedback zu geben.
Diese Verbesserungen sollten deine Anwendung nicht nur optisch ansprechender machen, sondern auch die Benutzererfahrung und die Wartbarkeit des Codes erheblich steigern.