Spaces:
Running
Running
File size: 8,388 Bytes
e727d96 | 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 | // Cart functionality
let cart = [];
let cartTotal = 0;
// Initialize
document.addEventListener('DOMContentLoaded', () => {
initializeFilters();
initializeNavbar();
});
// Filter functionality
function initializeFilters() {
const filterBtns = document.querySelectorAll('.filter-btn');
const products = document.querySelectorAll('.product-card');
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
// Update active state
filterBtns.forEach(b => {
b.classList.remove('bg-primary', 'text-white', 'border-primary');
b.classList.add('text-gray-700', 'border-gray-300');
});
btn.classList.remove('text-gray-700', 'border-gray-300');
btn.classList.add('bg-primary', 'text-white', 'border-primary');
// Filter products
const filter = btn.dataset.filter;
products.forEach(product => {
if (filter === 'all' || product.dataset.category === filter) {
product.style.display = 'block';
product.classList.add('fade-in');
} else {
product.style.display = 'none';
}
});
});
});
}
// Navbar scroll effect
function initializeNavbar() {
const navbar = document.getElementById('navbar');
let lastScroll = 0;
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > 100) {
navbar.classList.add('shadow-md');
} else {
navbar.classList.remove('shadow-md');
}
lastScroll = currentScroll;
});
}
// Mobile menu toggle
function toggleMobileMenu() {
const menu = document.getElementById('mobile-menu');
menu.classList.toggle('hidden');
}
// Cart toggle
function toggleCart() {
const sidebar = document.getElementById('cart-sidebar');
const overlay = document.getElementById('cart-overlay');
if (sidebar.classList.contains('cart-closed')) {
sidebar.classList.remove('cart-closed');
sidebar.classList.add('cart-open');
overlay.classList.remove('hidden');
document.body.style.overflow = 'hidden';
} else {
sidebar.classList.remove('cart-open');
sidebar.classList.add('cart-closed');
overlay.classList.add('hidden');
document.body.style.overflow = 'auto';
}
}
// Add to cart
function addToCart(id, name, price, image) {
const existingItem = cart.find(item => item.id === id);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({
id: id,
name: name,
price: price,
image: image,
quantity: 1
});
}
updateCart();
showToast(`${name} added to cart!`);
// Open cart automatically on first add
if (cart.length === 1 && cart[0].quantity === 1) {
setTimeout(() => {
const sidebar = document.getElementById('cart-sidebar');
if (sidebar.classList.contains('cart-closed')) {
toggleCart();
}
}, 500);
}
}
// Remove from cart
function removeFromCart(id) {
cart = cart.filter(item => item.id !== id);
updateCart();
}
// Update quantity
function updateQuantity(id, change) {
const item = cart.find(item => item.id === id);
if (item) {
item.quantity += change;
if (item.quantity <= 0) {
removeFromCart(id);
} else {
updateCart();
}
}
}
// Update cart UI
function updateCart() {
const cartItems = document.getElementById('cart-items');
const cartCount = document.getElementById('cart-count');
const cartTotalEl = document.getElementById('cart-total');
// Update count
const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
if (totalItems > 0) {
cartCount.textContent = totalItems;
cartCount.classList.remove('hidden');
} else {
cartCount.classList.add('hidden');
}
// Calculate total
cartTotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
cartTotalEl.textContent = `$${cartTotal.toFixed(2)}`;
// Render items
if (cart.length === 0) {
cartItems.innerHTML = `
<div class="text-center text-gray-500 py-12">
<i data-lucide="shopping-bag" class="w-16 h-16 mx-auto mb-4 text-gray-300"></i>
<p>Your cart is empty</p>
<button onclick="toggleCart()" class="mt-4 text-primary font-semibold hover:underline">Continue Shopping</button>
</div>
`;
} else {
cartItems.innerHTML = cart.map(item => `
<div class="flex gap-4 bg-gray-50 p-4 rounded-xl">
<img src="${item.image}" alt="${item.name}" class="w-20 h-20 object-cover rounded-lg">
<div class="flex-1">
<h3 class="font-semibold text-gray-900 line-clamp-1">${item.name}</h3>
<p class="text-primary font-bold mt-1">$${item.price.toFixed(2)}</p>
<div class="flex items-center mt-2 space-x-2">
<button onclick="updateQuantity(${item.id}, -1)" class="w-6 h-6 rounded-full bg-white border border-gray-300 flex items-center justify-center hover:bg-gray-100">
<i data-lucide="minus" class="w-3 h-3"></i>
</button>
<span class="font-medium w-8 text-center">${item.quantity}</span>
<button onclick="updateQuantity(${item.id}, 1)" class="w-6 h-6 rounded-full bg-white border border-gray-300 flex items-center justify-center hover:bg-gray-100">
<i data-lucide="plus" class="w-3 h-3"></i>
</button>
<button onclick="removeFromCart(${item.id})" class="ml-auto text-red-500 hover:text-red-700">
<i data-lucide="trash-2" class="w-4 h-4"></i>
</button>
</div>
</div>
</div>
`).join('');
}
// Reinitialize Lucide icons
lucide.createIcons();
}
// Checkout
function checkout() {
if (cart.length === 0) {
showToast('Your cart is empty!', 'error');
return;
}
showToast('Proceeding to checkout...', 'success');
setTimeout(() => {
alert('Thank you for your order! This is a demo checkout. Total: $' + cartTotal.toFixed(2));
cart = [];
updateCart();
toggleCart();
}, 1000);
}
// Toast notification
function showToast(message, type = 'success') {
const container = document.getElementById('toast-container');
const toast = document.createElement('div');
const bgColor = type === 'error' ? 'bg-red-500' : 'bg-green-500';
const icon = type === 'error' ? 'alert-circle' : 'check-circle';
toast.className = `toast ${bgColor} text-white px-6 py-3 rounded-full shadow-lg flex items-center space-x-2 min-w-[250px]`;
toast.innerHTML = `
<i data-lucide="${icon}" class="w-5 h-5"></i>
<span class="font-medium">${message}</span>
`;
container.appendChild(toast);
lucide.createIcons();
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateX(100%)';
toast.style.transition = 'all 0.3s ease';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// Newsletter
function handleNewsletter(e) {
e.preventDefault();
const email = e.target.querySelector('input[type="email"]').value;
showToast('Thank you for subscribing! Check your email for 15% off.');
e.target.reset();
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
// Close mobile menu if open
document.getElementById('mobile-menu').classList.add('hidden');
}
});
}); |