<div class="container">
<header>
<h1>🛍️ Shop</h1>
<div class="cart-icon" onclick="toggleCart()">
🛒 Cart
<span class="cart-count" id="cartCount">0</span>
</div>
</header>
<div class="products-grid" id="productsGrid"></div>
<div class="cart-modal" id="cartModal">
<div class="cart-content">
<div class="cart-header">
<h2>Shopping Cart</h2>
<button class="close-btn" onclick="toggleCart()">×</button>
</div>
<div id="cartItems"></div>
<div class="cart-total">
<div class="total-label">Total:</div>
<div class="total-amount" id="totalAmount">$0.00</div>
<button class="checkout-btn" onclick="checkout()">Checkout</button>
</div>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #808000 0%, #a0a040 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
background: white;
padding: 20px 30px;
border-radius: 10px;
margin-bottom: 30px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
h1 {
color: #808000;
font-size: 28px;
}
.cart-icon {
position: relative;
cursor: pointer;
background: linear-gradient(135deg, #808000 0%, #a0a040 100%);
color: white;
padding: 12px 24px;
border-radius: 8px;
font-weight: 600;
transition: transform 0.2s;
}
.cart-icon:hover {
transform: scale(1.05);
}
.cart-count {
position: absolute;
top: -8px;
right: -8px;
background: #d32f2f;
color: white;
border-radius: 50%;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-weight: bold;
}
.products-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
margin-bottom: 30px;
}
.product-card {
background: white;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: transform 0.3s;
}
.product-card:hover {
transform: translateY(-5px);
}
.product-image {
width: 100%;
height: 200px;
background: linear-gradient(135deg, #e0e0b0 0%, #f0f0d0 100%);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
font-size: 60px;
margin-bottom: 15px;
}
.product-title {
font-size: 18px;
color: #333;
margin-bottom: 10px;
}
.product-price {
font-size: 24px;
color: #808000;
font-weight: bold;
margin-bottom: 15px;
}
.add-to-cart-btn {
width: 100%;
background: linear-gradient(135deg, #808000 0%, #a0a040 100%);
color: white;
border: none;
padding: 12px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: opacity 0.3s;
}
.add-to-cart-btn:hover {
opacity: 0.9;
}
.cart-modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 1000;
align-items: center;
justify-content: center;
}
.cart-modal.active {
display: flex;
}
.cart-content {
background: white;
border-radius: 10px;
padding: 30px;
max-width: 600px;
width: 90%;
max-height: 80vh;
overflow-y: auto;
}
.cart-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 15px;
border-bottom: 2px solid #808000;
}
.cart-header h2 {
color: #808000;
}
.close-btn {
background: none;
border: none;
font-size: 28px;
cursor: pointer;
color: #666;
}
.cart-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 0;
border-bottom: 1px solid #eee;
}
.item-details {
flex: 1;
}
.item-name {
font-weight: 600;
color: #333;
margin-bottom: 5px;
}
.item-price {
color: #808000;
font-weight: bold;
}
.quantity-controls {
display: flex;
align-items: center;
gap: 10px;
}
.qty-btn {
width: 30px;
height: 30px;
border: none;
background: #808000;
color: white;
border-radius: 5px;
cursor: pointer;
font-weight: bold;
}
.qty-btn:hover {
background: #a0a040;
}
.remove-btn {
background: #d32f2f;
color: white;
border: none;
padding: 8px 16px;
border-radius: 5px;
cursor: pointer;
margin-left: 10px;
}
.cart-total {
margin-top: 20px;
padding-top: 20px;
border-top: 2px solid #808000;
text-align: right;
}
.total-label {
font-size: 20px;
color: #333;
margin-bottom: 10px;
}
.total-amount {
font-size: 32px;
color: #808000;
font-weight: bold;
}
.checkout-btn {
width: 100%;
background: linear-gradient(135deg, #808000 0%, #a0a040 100%);
color: white;
border: none;
padding: 15px;
border-radius: 8px;
font-size: 18px;
font-weight: 600;
cursor: pointer;
margin-top: 20px;
}
.empty-cart {
text-align: center;
padding: 40px;
color: #999;
}
const products = [
{ id: 1, name: 'Wireless Headphones', price: 79.99, emoji: '🎧' },
{ id: 2, name: 'Smart Watch', price: 199.99, emoji: '⌚' },
{ id: 3, name: 'Laptop', price: 999.99, emoji: '💻' },
{ id: 4, name: 'Camera', price: 549.99, emoji: '📷' },
{ id: 5, name: 'Smartphone', price: 699.99, emoji: '📱' },
{ id: 6, name: 'Tablet', price: 449.99, emoji: '📱' }
];
let cart = [];
function renderProducts() {
const grid = document.getElementById('productsGrid');
grid.innerHTML = products.map(product => `
<div class="product-card">
<div class="product-image">${product.emoji}</div>
<div class="product-title">${product.name}</div>
<div class="product-price">$${product.price.toFixed(2)}</div>
<button class="add-to-cart-btn" onclick="addToCart(${product.id})">
Add to Cart
</button>
</div>
`).join('');
}
function addToCart(productId) {
const product = products.find(p => p.id === productId);
const existingItem = cart.find(item => item.id === productId);
if (existingItem) {
existingItem.quantity++;
} else {
cart.push({ ...product, quantity: 1 });
}
updateCart();
}
function updateQuantity(productId, change) {
const item = cart.find(item => item.id === productId);
if (item) {
item.quantity += change;
if (item.quantity <= 0) {
removeFromCart(productId);
} else {
updateCart();
}
}
}
function removeFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
updateCart();
}
function updateCart() {
const cartCount = document.getElementById('cartCount');
const cartItems = document.getElementById('cartItems');
const totalAmount = document.getElementById('totalAmount');
const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
cartCount.textContent = totalItems;
if (cart.length === 0) {
cartItems.innerHTML = '<div class="empty-cart">Your cart is empty</div>';
totalAmount.textContent = '$0.00';
return;
}
cartItems.innerHTML = cart.map(item => `
<div class="cart-item">
<div class="item-details">
<div class="item-name">${item.emoji} ${item.name}</div>
<div class="item-price">$${item.price.toFixed(2)}</div>
</div>
<div class="quantity-controls">
<button class="qty-btn" onclick="updateQuantity(${item.id}, -1)">−</button>
<span>${item.quantity}</span>
<button class="qty-btn" onclick="updateQuantity(${item.id}, 1)">+</button>
<button class="remove-btn" onclick="removeFromCart(${item.id})">Remove</button>
</div>
</div>
`).join('');
const total = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
totalAmount.textContent = `$${total.toFixed(2)}`;
}
function toggleCart() {
const modal = document.getElementById('cartModal');
modal.classList.toggle('active');
}
function checkout() {
if (cart.length === 0) {
alert('Your cart is empty!');
return;
}
const total = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
alert(`Checkout successful! Total: $${total.toFixed(2)}`);
cart = [];
updateCart();
toggleCart();
}
renderProducts();
No comments yet. Be the first!