Dark MOde/Theme Veränderung/form Validation
This commit is contained in:
141
script.js
Normal file
141
script.js
Normal file
@@ -0,0 +1,141 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
const navLinks = document.querySelectorAll('nav a');
|
||||
navLinks.forEach(link => {
|
||||
link.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
const targetId = this.getAttribute('href');
|
||||
const targetSection = document.querySelector(targetId);
|
||||
|
||||
if (targetSection) {
|
||||
targetSection.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
block: 'start'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//Dark MOde
|
||||
const darkModeBtn = document.getElementById('darkModeBtn');
|
||||
|
||||
if (darkModeBtn) {
|
||||
if (localStorage.getItem('darkMode') === 'enabled') {
|
||||
document.body.classList.add('dark-mode');
|
||||
darkModeBtn.textContent = ' Light Mode';
|
||||
}
|
||||
|
||||
darkModeBtn.addEventListener('click', function() {
|
||||
document.body.classList.toggle('dark-mode');
|
||||
|
||||
if (document.body.classList.contains('dark-mode')) {
|
||||
localStorage.setItem('darkMode', 'enabled');
|
||||
darkModeBtn.textContent = ' Light Mode';
|
||||
} else {
|
||||
localStorage.setItem('darkMode', 'disabled');
|
||||
darkModeBtn.textContent = ' Dark Mode';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//Them changer
|
||||
const colorButtons = document.querySelectorAll('.color-btn');
|
||||
|
||||
colorButtons.forEach(button => {
|
||||
button.addEventListener('click', function() {
|
||||
const color = this.getAttribute('data-color');
|
||||
const darkColor = this.getAttribute('data-color-dark');
|
||||
|
||||
document.documentElement.style.setProperty('--main-color', color);
|
||||
document.documentElement.style.setProperty('--main-color-dark', darkColor);
|
||||
|
||||
localStorage.setItem('themeColor', color);
|
||||
localStorage.setItem('themeColorDark', darkColor);
|
||||
|
||||
colorButtons.forEach(btn => btn.classList.remove('active'));
|
||||
this.classList.add('active');
|
||||
});
|
||||
});
|
||||
|
||||
const savedColor = localStorage.getItem('themeColor');
|
||||
const savedColorDark = localStorage.getItem('themeColorDark');
|
||||
|
||||
if (savedColor && savedColorDark) {
|
||||
document.documentElement.style.setProperty('--main-color', savedColor);
|
||||
document.documentElement.style.setProperty('--main-color-dark', savedColorDark);
|
||||
|
||||
colorButtons.forEach(btn => {
|
||||
if (btn.getAttribute('data-color') === savedColor) {
|
||||
btn.classList.add('active');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Validation of FORM
|
||||
const form = document.querySelector('#contact form');
|
||||
|
||||
if (form) {
|
||||
const nameInput = form.querySelector('input[name="name"]');
|
||||
const emailInput = form.querySelector('input[name="email"]');
|
||||
const messageInput = form.querySelector('textarea[name="message"]');
|
||||
|
||||
nameInput.addEventListener('input', function() {
|
||||
if (this.value.trim().length > 2) {
|
||||
this.style.borderColor = '#4caf50';
|
||||
} else {
|
||||
this.style.borderColor = '#e0e0e0';
|
||||
}
|
||||
});
|
||||
|
||||
emailInput.addEventListener('input', function() {
|
||||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (emailPattern.test(this.value)) {
|
||||
this.style.borderColor = '#4caf50';
|
||||
} else {
|
||||
this.style.borderColor = '#f44336';
|
||||
}
|
||||
});
|
||||
|
||||
messageInput.addEventListener('input', function() {
|
||||
if (this.value.trim().length >= 10) {
|
||||
this.style.borderColor = '#4caf50';
|
||||
} else {
|
||||
this.style.borderColor = '#e0e0e0';
|
||||
}
|
||||
});
|
||||
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const name = nameInput.value.trim();
|
||||
const email = emailInput.value.trim();
|
||||
const message = messageInput.value.trim();
|
||||
|
||||
if (name.length < 3) {
|
||||
alert('Bitte geben Sie eine gültige E-Mail-Adresse ein!');
|
||||
nameInput.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
if (!emailPattern.test(email)) {
|
||||
alert('Der Name muss mindestens 3 Zeichen lang sein!');
|
||||
emailInput.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.length < 10) {
|
||||
alert('Die Nachricht muss mindestens 10 Zeichen lang sein!');
|
||||
messageInput.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
alert('Vielen Dank, ' + name + '! Ihre Nachricht wurde gesendet.');
|
||||
form.reset();
|
||||
|
||||
nameInput.style.borderColor = '#e0e0e0';
|
||||
emailInput.style.borderColor = '#e0e0e0';
|
||||
messageInput.style.borderColor = '#e0e0e0';
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user