File size: 9,760 Bytes
66b8358 967e3bf dd96a33 c3e668d dd96a33 66b8358 967e3bf 66b8358 967e3bf 66b8358 967e3bf 66b8358 512174b 66b8358 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
document.addEventListener('DOMContentLoaded', function() {
// Initialize cart functionality
initializeCart();
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth'
});
}
});
});
// Newsletter form submission
const newsletterForm = document.querySelector('section.bg-gradient-to-r.from-gray-900.to-gray-800');
if (newsletterForm) {
const input = newsletterForm.querySelector('input[type="email"]');
const button = newsletterForm.querySelector('button[type="submit"]');
if (button && input) {
button.addEventListener('click', function(e) {
e.preventDefault();
if (input.value && input.value.includes('@')) {
alert('Vielen Dank für Ihre Anmeldung zum Newsletter!');
input.value = '';
} else {
alert('Bitte geben Sie eine gültige E-Mail-Adresse ein.');
}
});
}
}
});
function initializeCart() {
// Initialize cart count from localStorage
if (!localStorage.getItem('cartItems')) {
localStorage.setItem('cartItems', JSON.stringify([]));
}
if (!localStorage.getItem('cartCount')) {
localStorage.setItem('cartCount', '0');
}
// Add to cart buttons
document.addEventListener('click', function(e) {
if (e.target.classList.contains('add-to-cart-btn') || e.target.closest('.add-to-cart-btn')) {
e.preventDefault();
const button = e.target.classList.contains('add-to-cart-btn') ? e.target : e.target.closest('.add-to-cart-btn');
const product = button.getAttribute('data-product');
const price = parseFloat(button.getAttribute('data-price'));
const image = button.getAttribute('data-image');
addToCart(product, price, image);
}
// Buy now buttons
if (e.target.classList.contains('buy-now-btn') || e.target.closest('.buy-now-btn')) {
e.preventDefault();
const button = e.target.classList.contains('buy-now-btn') ? e.target : e.target.closest('.buy-now-btn');
const product = button.getAttribute('data-product');
const price = parseFloat(button.getAttribute('data-price'));
const image = button.getAttribute('data-image') || 'http://static.photos/technology/640x360/1';
addToCart(product, price, image);
// Open cart after adding
const cart = document.querySelector('custom-cart');
if (cart) cart.openCart();
}
});
// Update cart count display
updateCartCountDisplay();
}
function addToCart(product, price, image) {
let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
let existingItem = cartItems.find(item => item.product === product);
if (existingItem) {
existingItem.quantity += 1;
} else {
cartItems.push({
product: product,
price: price,
image: image,
quantity: 1
});
}
localStorage.setItem('cartItems', JSON.stringify(cartItems));
// Update cart count
let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0);
localStorage.setItem('cartCount', totalCount.toString());
// Update cart count display
updateCartCountDisplay();
// Trigger event for navbar to update
window.dispatchEvent(new Event('cartUpdated'));
// Update cart UI if open
updateCartUI();
// Show confirmation
showNotification(`${product} wurde zum Warenkorb hinzugefügt!`);
}
function updateCartCountDisplay() {
const count = localStorage.getItem('cartCount') || '0';
// Update in navbar shadow DOM
const navbar = document.querySelector('custom-navbar');
if (navbar) {
const cartCountElement = navbar.shadowRoot.getElementById('cart-count');
const mobileCartCountElement = navbar.shadowRoot.getElementById('mobile-cart-count');
if (cartCountElement) cartCountElement.textContent = count;
if (mobileCartCountElement) mobileCartCountElement.textContent = count;
}
}
function updateCartUI() {
const cart = document.querySelector('custom-cart');
if (!cart) return;
const cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
const cartItemsContainer = cart.shadowRoot.querySelector('.cart-items');
const subtotalElement = cart.shadowRoot.querySelector('.subtotal span:last-child');
if (!cartItemsContainer) return;
// Clear existing items
cartItemsContainer.innerHTML = '';
let subtotal = 0;
cartItems.forEach((item, index) => {
const itemTotal = item.price * item.quantity;
subtotal += itemTotal;
const itemElement = document.createElement('div');
itemElement.className = 'cart-item';
itemElement.innerHTML = `
<img src="${item.image}" alt="${item.product}" class="cart-item-img">
<div class="cart-item-details">
<div class="cart-item-title">${item.product}</div>
<div class="cart-item-price">€${item.price.toFixed(2)}</div>
<div class="quantity-selector">
<button class="quantity-btn minus" data-index="${index}">−</button>
<input type="number" value="${item.quantity}" min="1" class="quantity-input" data-index="${index}">
<button class="quantity-btn plus" data-index="${index}">+</button>
</div>
<div class="delete-btn" data-index="${index}">Löschen</div>
</div>
`;
cartItemsContainer.appendChild(itemElement);
});
if (subtotalElement) {
subtotalElement.textContent = `€${subtotal.toFixed(2)}`;
}
// Add event listeners for quantity buttons
cartItemsContainer.querySelectorAll('.quantity-btn').forEach(btn => {
btn.addEventListener('click', function() {
const index = parseInt(this.getAttribute('data-index'));
updateCartItemQuantity(index, this.classList.contains('plus') ? 1 : -1);
});
});
cartItemsContainer.querySelectorAll('.quantity-input').forEach(input => {
input.addEventListener('change', function() {
const index = parseInt(this.getAttribute('data-index'));
const newQuantity = parseInt(this.value) || 1;
updateCartItemQuantity(index, 0, newQuantity);
});
});
cartItemsContainer.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', function() {
const index = parseInt(this.getAttribute('data-index'));
removeFromCart(index);
});
});
}
function updateCartItemQuantity(index, change, newQuantity = null) {
let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
if (index >= 0 && index < cartItems.length) {
if (newQuantity !== null) {
cartItems[index].quantity = Math.max(1, newQuantity);
} else {
cartItems[index].quantity = Math.max(1, cartItems[index].quantity + change);
}
localStorage.setItem('cartItems', JSON.stringify(cartItems));
// Update cart count
let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0);
localStorage.setItem('cartCount', totalCount.toString());
// Update displays
updateCartCountDisplay();
updateCartUI();
window.dispatchEvent(new Event('cartUpdated'));
}
}
function removeFromCart(index) {
let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];
if (index >= 0 && index < cartItems.length) {
cartItems.splice(index, 1);
localStorage.setItem('cartItems', JSON.stringify(cartItems));
// Update cart count
let totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0);
localStorage.setItem('cartCount', totalCount.toString());
// Update displays
updateCartCountDisplay();
updateCartUI();
window.dispatchEvent(new Event('cartUpdated'));
showNotification('Artikel wurde aus dem Warenkorb entfernt!');
}
}
function showNotification(message) {
// Create notification element
const notification = document.createElement('div');
notification.className = 'fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg z-50 transform transition-transform duration-300 translate-x-full';
notification.textContent = message;
notification.id = 'cart-notification';
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.classList.remove('translate-x-full');
notification.classList.add('translate-x-0');
}, 10);
// Remove after 3 seconds
setTimeout(() => {
notification.classList.remove('translate-x-0');
notification.classList.add('translate-x-full');
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 300);
}, 3000);
}
|