html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simulated Walmart - A Blessing to Use</title>
<style>
/* Reset & base */
* {
box-sizing: border-box;
}
body, html {
margin:0; padding:0;
height: 100%;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #2e8bff, #1d3f99);
color: #f0f8ff;
overflow-x: hidden;
-webkit-font-smoothing: antialiased;
}
/* Animations library (simplified demo with many keyframes) */
@keyframes fadeInSlideUp {
0% {opacity:0; transform: translateY(20px);}
100% {opacity:1; transform: translateY(0);}
}
@keyframes pulseGlow {
0%, 100% {box-shadow: 0 0 10px #4fc3f7;}
50% {box-shadow: 0 0 20px #00b0ff;}
}
@keyframes rotate360 {
0% {transform: rotate(0);}
100% {transform: rotate(360deg);}
}
@keyframes slideInLeft {
0% {opacity:0; transform: translateX(-100%);}
100% {opacity:1; transform: translateX(0);}
}
@keyframes slideInRight {
0% {opacity:0; transform: translateX(100%);}
100% {opacity:1; transform: translateX(0);}
}
@keyframes bounceUpDown {
0%, 100% {transform: translateY(0);}
50% {transform: translateY(-10px);}
}
@keyframes shimmer {
0% {background-position: -200% 0;}
100% {background-position: 200% 0;}
}
@keyframes scaleUpDown {
0%, 100% {transform: scale(1);}
50% {transform: scale(1.05);}
}
/* And many more animations, combined and assigned as needed */
/* Utility animations - we will randomly assign to elements */
.anim-fade-in-up {
animation: fadeInSlideUp 0.8s ease forwards;
}
.anim-pulse-glow {
animation: pulseGlow 3s infinite ease-in-out;
}
.anim-rotate360 {
animation: rotate360 8s linear infinite;
}
.anim-slide-in-left {
animation: slideInLeft 1s ease forwards;
}
.anim-slide-in-right {
animation: slideInRight 1s ease forwards;
}
.anim-bounce {
animation: bounceUpDown 2s ease-in-out infinite;
}
.anim-shimmer {
background: linear-gradient(90deg, #0b71f1 25%, #4fc3f7 37%, #0b71f1 63%);
background-size: 200% 100%;
animation: shimmer 3s linear infinite;
color: transparent;
background-clip: text;
-webkit-background-clip: text;
}
.anim-scale {
animation: scaleUpDown 4s ease-in-out infinite;
}
/* Header styles */
header {
position: sticky;
top: 0;
z-index: 999;
background: rgba(13, 66, 151, 0.8);
backdrop-filter: blur(8px);
border-bottom: 1px solid rgba(255 255 255 / 0.1);
display: flex;
align-items: center;
padding: 0 20px;
height: 60px;
user-select: none;
}
header h1 {
font-weight: 800;
font-size: 1.8rem;
color: #ffd300;
text-shadow:
1px 1px 3px rgba(0,0,0,0.8),
0 0 10px #ffd300;
flex: 1;
cursor: default;
letter-spacing: 1.3px;
}
/* Nav (fake main menu) */
nav {
gap: 15px;
display: flex;
align-items: center;
}
nav button {
background: rgba(255 255 255 / 0.15);
border: none;
border-radius: 12px;
backdrop-filter: blur(10px);
color: white;
padding: 8px 14px;
font-weight: 600;
font-size: 0.9rem;
cursor: pointer;
transition: background-color 0.35s ease, transform 0.25s ease;
user-select: none;
box-shadow: 0 0 10px #4fc3f7;
position: relative;
overflow: hidden;
}
nav button::before {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 0; height: 0;
border-radius: 50%;
transform: translate(-50%, -50%);
background: #00a0ff55;
transition: width 0.5s ease, height 0.5s ease;
z-index: 0;
}
nav button:hover::before {
width: 200%;
height: 200%;
}
nav button:hover {
background-color: #0087e3cc;
transform: scale(1.07);
box-shadow:
0 0 15px #64b5f6,
0 0 35px #2979ff99;
}
nav button:focus-visible {
outline-offset: 2px;
outline: 2px solid #ffd300aa;
}
/* Main container */
main {
display: flex;
height: calc(100vh - 60px);
overflow: hidden;
}
/* Sidebar - categories */
aside.categories {
width: 280px;
background: rgba(10, 50, 110, 0.7);
backdrop-filter: blur(12px);
border-right: 1px solid rgba(255 255 255 / 0.1);
display: flex;
flex-direction: column;
padding: 20px 15px;
gap: 12px;
color: #cbe6ff;
user-select: none;
}
aside.categories h2 {
margin: 0 0 12px 0;
font-weight: 700;
font-size: 1.2rem;
color: #ffd300;
text-shadow: 0 0 8px #ffd300aa;
}
aside.categories button {
background: transparent;
border: none;
color: inherit;
text-align: left;
font-weight: 600;
font-size: 1rem;
padding: 10px 12px;
border-radius: 12px;
cursor: pointer;
transition: background-color 0.35s ease, transform 0.2s ease;
box-shadow: inset 0 0 6px rgba(255 255 255 / 0.1);
}
aside.categories button:hover, aside.categories button.active {
background: #0058a3cc;
box-shadow:
0 0 15px #64b5f6,
inset 0 0 12px #64b5f6cc;
transform: scale(1.04);
color: #ffd300;
}
/* Products area */
section.products {
flex: 1;
background: rgba(15, 38, 75, 0.6);
backdrop-filter: blur(15px);
padding: 24px 30px;
display: grid;
grid-template-columns: repeat(auto-fill,minmax(240px,1fr));
gap: 26px;
overflow-y: auto;
scroll-behavior: smooth;
}
/* Product card */
.product-card {
background: rgba(255 255 255 / 0.1);
border-radius: 20px;
padding: 20px 16px;
display: flex;
flex-direction: column;
align-items: center;
box-shadow: 0 0 10px #2699ff88;
cursor: pointer;
transition: box-shadow 0.35s ease, transform 0.25s ease;
user-select: none;
position: relative;
overflow: hidden;
}
.product-card:hover {
box-shadow:
0 0 25px #4fc3f7,
0 0 40px #00b0ffcc;
transform: translateY(-6px) scale(1.05);
}
.product-card img {
width: 140px;
height: 140px;
object-fit: contain;
filter: drop-shadow(0 0 3px #00a0ffaa);
margin-bottom: 14px;
}
.product-name {
font-weight: 700;
font-size: 1.05rem;
text-align: center;
color: #cbe6ff;
min-height: 44px;
margin-bottom: 8px;
user-select: text;
}
.product-price {
font-weight: 800;
font-size: 1.2rem;
color: #ffd300;
margin-bottom: 14px;
}
.add-cart-btn {
background: transparent;
border: 2px solid #00a0ff;
color: #00a8ff;
font-weight: 700;
border-radius: 14px;
padding: 10px 18px;
cursor: pointer;
transition: background-color 0.35s ease, color 0.35s ease, transform 0.25s ease;
user-select: none;
width: 100%;
text-align: center;
box-shadow: 0 0 5px #00a0ff66 inset;
}
.add-cart-btn:hover {
background-color: #00a0ffdd;
color: #003d5f;
transform: scale(1.07);
box-shadow:
0 0 12px #00a0ff,
inset 0 0 15px #00a0ffaa;
}
.add-cart-btn:active {
transform: scale(0.95);
}
/* Cart overlay - right panel */
aside.cart {
width: 320px;
background: rgba(10, 50, 110, 0.8);
backdrop-filter: blur(18px);
border-left: 1px solid rgba(255 255 255 / 0.1);
color: #cbe6ff;
display: flex;
flex-direction: column;
padding: 20px 18px;
overflow-y: auto;
user-select: none;
}
aside.cart h2 {
font-weight: 700;
font-size: 1.3rem;
margin-bottom: 14px;
color: #ffd300;
text-shadow: 0 0 10px #ffd300cc;
}
.cart-empty {
flex: 1;
font-style: italic;
text-align: center;
color: #92b8ffaa;
margin-top: 40px;
}
.cart-items {
flex: 1;
overflow-y: auto;
margin-bottom: 10px;
display: flex;
flex-direction: column;
gap: 12px;
}
.cart-item {
background: rgba(255 255 255 / 0.1);
border-radius: 14px;
padding: 12px 15px;
display: flex;
align-items: center;
box-shadow: 0 0 6px #0077dd66;
}
.cart-item img {
width: 60px;
height: 60px;
object-fit: contain;
margin-right: 12px;
filter: drop-shadow(0 0 3px #2699ffbb);
border-radius: 8px;
}
.cart-item-info {
flex:1;
}
.cart-item-name {
font-weight: 600;
font-size: 1rem;
color: #cbe6ff;
margin-bottom: 4px;
}
.cart-item-price-qty {
font-size: 0.85rem;
color: #92b8ffcc;
display: flex;
justify-content: space-between;
align-items: center;
}
.cart-item-price {
font-weight: 700;
color: #ffd300;
}
.cart-item-controls {
display: flex;
gap: 8px;
}
.cart-btn {
background: transparent;
border: none;
color: #cbe6ff;
cursor: pointer;
font-size: 1.2rem;
user-select: none;
border-radius: 10px;
transition: background-color 0.3s ease;
padding: 3px 8px;
line-height: 1;
}
.cart-btn:hover {
background: #00a0ff44;
color: #ffd300;
}
/* Total and checkout */
.cart-total {
font-weight: 700;
font-size: 1.3rem;
text-align: right;
margin-bottom: 12px;
color: #ffd300;
text-shadow: 0 0 8px #ffd300cc;
}
.btn-checkout {
background: linear-gradient(90deg, #ffd300, #ffec66);
border: none;
color: #003300;
font-weight: 800;
font-size: 1.1rem;
padding: 12px 18px;
border-radius: 20px;
cursor: pointer;
box-shadow:
0 0 12px #ffd300aa,
inset 0 0 30px #ffec66cc;
user-select: none;
transition: transform 0.35s ease, box-shadow 0.5s ease;
text-shadow: 0 0 6px #403300;
}
.btn-checkout:hover {
transform: scale(1.07);
box-shadow:
0 0 20px #ffd300ee,
inset 0 0 50px #ffec66ff;
}
.btn-checkout:active {
transform: scale(0.95);
}
/* Modal styles for checkout */
.modal-bg {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.7);
backdrop-filter: blur(10px);
display: flex;
justify-content: center;
align-items: center;
opacity: 0;
pointer-events: none;
transition: opacity 0.35s ease;
z-index: 1500;
}
.modal-bg.active {
opacity: 1;
pointer-events: all;
}
.modal-window {
background: rgba(20, 70, 150, 0.95);
border-radius: 24px;
width: 380px;
max-width: 90vw;
padding: 24px 30px;
box-shadow: 0 0 30px #00a0ffcc;
color: #c8deff;
user-select: text;
position: relative;
animation: fadeInSlideUp 0.5s ease forwards;
}
.modal-window h3 {
margin-top: 0;
margin-bottom: 20px;
color: #ffd300;
text-shadow: 0 0 14px #ffd300dd;
}
.modal-close {
position: absolute;
top: 14px;
right: 14px;
background: transparent;
border: none;
font-size: 1.6rem;
color: #ffd300cc;
font-weight: 900;
cursor: pointer;
user-select: none;
transition: color 0.3s ease;
}
.modal-close:hover {
color: #ffd300ee;
}
.modal-content {
max-height: 60vh;
overflow-y: auto;
}
/* Form inside modal */
form {
display: flex;
flex-direction: column;
gap: 16px;
}
form label {
font-weight: 600;
}
form input, form select {
border: none;
border-radius: 12px;
padding: 10px 14px;
font-size: 1rem;
font-family: inherit;
outline: none;
user-select: text;
color: #003344;
}
form input:focus, form select:focus {
outline: 2px solid #ffd300;
}
.btn-submit {
background: #ffd300cc;
border: none;
padding: 12px 16px;
font-weight: 800;
border-radius: 20px;
cursor: pointer;
color: #003300;
font-size: 1.1rem;
user-select: none;
box-shadow: 0 0 12px #ffd300dd;
transition: background-color 0.3s ease;
}
.btn-submit:hover {
background: #ffec66;
}
/* Delivery simulation */
.delivery-status {
background: rgba(0, 140, 255, 0.7);
border-radius: 18px;
color: #dbefff;
font-weight: 700;
padding: 14px 22px;
margin-top: 20px;
text-align:center;
font-size: 1.05rem;
box-shadow:
0 0 15px #39aaffbb,
0 0 30px #00a0ff88 inset;
user-select: none;
}
.delivery-status span {
font-style: italic;
color: #ffd300;
}
/* Scrollbar styles */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-thumb {
background: rgba(0,160,255,0.6);
border-radius: 12px;
box-shadow: inset 0 0 6px #00a0ff99;
}
::-webkit-scrollbar-track {
background: rgba(5, 20, 40, 0.4);
}
/* Responsive */
@media (max-width: 1120px) {
aside.categories {
display: none;
}
main {
flex-direction: column;
height: auto;
}
section.products {
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
height: auto;
padding: 12px 15px;
}
aside.cart {
width: 100%;
height: 320px;
border-left: none;
border-top: 1px solid rgba(255 255 255 / 0.1);
padding: 14px 15px;
flex-shrink: 0;
}
}
@media (max-width: 600px) {
nav {
display:none;
}
}
</style>
</head>
<body>
<header>
<h1 class="anim-shimmer anim-scale">Walmart Simulated</h1>
<nav aria-label="Main Navigation" class="anim-fade-in-up" style="animation-delay:.3s">
<button type="button" id="nav-home" aria-current="page" tabindex="0">Home</button>
<button type="button" id="nav-cart">Cart (<span id="nav-cart-count">0</span>)</button>
<button type="button" id="nav-checkout">Checkout</button>
</nav>
</header>
<main>
<aside class="categories anim-slide-in-left" aria-label="Product Categories">
<h2>Categories</h2>
<button type="button" class="category-btn active" data-category="all" tabindex="0">All Products</button>
<button type="button" class="category-btn" data-category="electronics">Electronics</button>
<button type="button" class="category-btn" data-category="toys">Toys</button>
<button type="button" class="category-btn" data-category="clothing">Clothing</button>
<button type="button" class="category-btn" data-category="groceries">Groceries</button>
<button type="button" class="category-btn" data-category="home">Home</button>
<button type="button" class="category-btn" data-category="sports">Sports</button>
</aside>
<section class="products anim-slide-in-right" aria-live="polite" aria-label="Products List" tabindex="0" id="products-container">
<!-- Products will be dynamically inserted here -->
</section>
<aside class="cart anim-fade-in-up" aria-label="Shopping Cart" role="complementary" id="cart-panel">
<h2>Shopping Cart</h2>
<div class="cart-items" id="cart-items">
<p class="cart-empty" aria-live="assertive">Your cart is empty</p>
</div>
<div aria-live="polite" id="cart-total" class="cart-total" tabindex="0">Total: $0.00</div>
<button class="btn-checkout" id="btn-checkout-panel" disabled>Proceed to Checkout</button>
</aside>
</main>
<!-- Checkout Modal -->
<div class="modal-bg" id="checkout-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="checkoutTitle">
<div class="modal-window anim-fade-in-up" role="document">
<button aria-label="Close Checkout Modal" class="modal-close" id="modal-close">×</button>
<h3 id="checkoutTitle">Checkout</h3>
<div class="modal-content" id="checkout-content">
<!-- Checkout form or summary will be rendered here -->
<form id="checkout-form" novalidate>
<label for="name">Full Name</label>
<input type="text" id="name" name="name" placeholder="John Doe" required autocomplete="name" />
<label for="address">Delivery Address</label>
<input type="text" id="address" name="address" placeholder="123 Main St, City, State" required autocomplete="street-address" />
<label for="delivery-method">Delivery Method</label>
<select id="delivery-method" name="delivery-method" required>
<option value="" disabled selected>Select option</option>
<option value="standard">Standard (3-5 days)</option>
<option value="express">Express (1-2 days)</option>
<option value="pickup">Pickup in-store</option>
</select>
<label for="payment-method">Payment Method</label>
<select id="payment-method" name="payment-method" required>
<option value="" disabled selected>Select option</option>
<option value="credit-card">Credit Card</option>
<option value="paypal">PayPal</option>
<option value="cash">Cash on Delivery</option>
</select>
<button type="submit" class="btn-submit">Confirm Order</button>
</form>
</div>
<div class="delivery-status" id="delivery-status" style="display:none;"></div>
</div>
</div>
<script>
(() => {
"use strict";
const products = [
// Electronics
{
id: 'p1',
name: '4K Ultra HD Smart TV',
category: 'electronics',
price: 499.99,
image: 'https://cdn-icons-png.flaticon.com/512/823/823431.png',
alt: "4K Ultra HD Smart TV",
},
{
id: 'p2',
name: 'Wireless Bluetooth Headphones',
category: 'electronics',
price: 89.99,
image: 'https://cdn-icons-png.flaticon.com/512/727/727245.png',
alt: "Wireless Bluetooth Headphones",
},
{
id: 'p3',
name: 'Portable Charger 10000mAh',
category: 'electronics',
price: 29.99,
image: 'https://cdn-icons-png.flaticon.com/512/251/251022.png',
alt: "Portable Charger",
},
// Toys
{
id: 'p4',
name: 'Remote Control Car',
category: 'toys',
price: 45.50,
image: 'https://cdn-icons-png.flaticon.com/512/1332/1332477.png',
alt: "Remote Control Car",
},
{
id: 'p5',
name: 'Lego City Fire Station',
category: 'toys',
price: 79.00,
image: 'https://cdn-icons-png.flaticon.com/512/1006/1006734.png',
alt: "Lego Fire Station",
},
{
id: 'p6',
name: 'Dollhouse Furniture Set',
category: 'toys',
price: 39.99,
image: 'https://cdn-icons-png.flaticon.com/512/708/708407.png',
alt: "Dollhouse Furniture",
},
// Clothing
{
id: 'p7',
name: 'Men\'s Running Shoes',
category: 'clothing',
price: 59.99,
image: 'https://cdn-icons-png.flaticon.com/512/747/747376.png',
alt: "Men's Running Shoes",
},
{
id: 'p8',
name: 'Women\'s Casual Jacket',
category: 'clothing',
price: 85.25,
image: 'https://cdn-icons-png.flaticon.com/512/892/892458.png',
alt: "Women's Casual Jacket",
},
{
id: 'p9',
name: 'Unisex Baseball Cap',
category: 'clothing',
price: 15.00,
image: 'https://cdn-icons-png.flaticon.com/512/1055/1055646.png',
alt: "Unisex Baseball Cap",
},
// Groceries
{
id: 'p10',
name: 'Organic Apples (1 lb)',
category: 'groceries',
price: 3.99,
image: 'https://cdn-icons-png.flaticon.com/512/415/415733.png',
alt: "Organic Apples",
},
{
id: 'p11',
name: 'Fresh Milk (1 Gallon)',
category: 'groceries',
price: 2.69,
image: 'https://cdn-icons-png.flaticon.com/512/135/135620.png',
alt: "Fresh Milk",
},
{
id: 'p12',
name: 'Brown Bread Loaf',
category: 'groceries',
price: 2.29,
image: 'https://cdn-icons-png.flaticon.com/512/416/416140.png',
alt: "Brown Bread Loaf",
},
// Home
{
id: 'p13',
name: 'LED Desk Lamp',
category: 'home',
price: 22.49,
image: 'https://cdn-icons-png.flaticon.com/512/2920/2920032.png',
alt: "LED Desk Lamp",
},
{
id: 'p14',
name: 'Memory Foam Pillow',
category: 'home',
price: 19.99,
image: 'https://cdn-icons-png.flaticon.com/512/2307/2307473.png',
alt: "Memory Foam Pillow",
},
{
id: 'p15',
name: 'Silicone Baking Mat',
category: 'home',
price: 9.99,
image: 'https://cdn-icons-png.flaticon.com/512/1458/1458726.png',
alt: "Silicone Baking Mat",
},
// Sports
{
id: 'p16',
name: 'Yoga Mat Non-Slip',
category: 'sports',
price: 28.75,
image: 'https://cdn-icons-png.flaticon.com/512/1883/1883687.png',
alt: "Yoga Mat",
},
{
id: 'p17',
name: 'Basketball Official Size',
category: 'sports',
price: 25.00,
image: 'https://cdn-icons-png.flaticon.com/512/355/355989.png',
alt: "Basketball",
},
{
id: 'p18',
name: 'Tennis Racket',
category: 'sports',
price: 65.50,
image: 'https://cdn-icons-png.flaticon.com/512/3075/3075857.png',
alt: "Tennis Racket",
},
];
// Variables, elements
const productsContainer = document.getElementById('products-container');
const cartItemsContainer = document.getElementById('cart-items');
const cartTotalDisplay = document.getElementById('cart-total');
const btnCheckoutPanel = document.getElementById('btn-checkout-panel');
const navCartCount = document.getElementById('nav-cart-count');
const checkoutModal = document.getElementById('checkout-modal');
const modalCloseBtn = document.getElementById('modal-close');
const checkoutForm = document.getElementById('checkout-form');
const deliveryStatus = document.getElementById('delivery-status');
const categoryButtons = document.querySelectorAll('.category-btn');
let cart = JSON.parse(localStorage.getItem('walmartSimCart')) || {};
// Helpers
function formatPrice(num) {
return '$' + num.toFixed(2);
}
function saveCart() {
localStorage.setItem('walmartSimCart', JSON.stringify(cart));
}
// Render Functions
function renderProducts(filterCategory = 'all') {
productsContainer.innerHTML = '';
let filtered = filterCategory === 'all' ? products : products.filter(p => p.category === filterCategory);
if(filtered.length === 0) {
const noProductMsg = document.createElement('p');
noProductMsg.textContent = "No products found in this category.";
noProductMsg.style.fontStyle = 'italic';
noProductMsg.style.color = '#89bbffaa';
noProductMsg.style.gridColumn = '1 / -1';
productsContainer.appendChild(noProductMsg);
return;
}
filtered.forEach((product, i) => {
const card = document.createElement('article');
card.className = 'product-card anim-fade-in-up';
card.style.animationDelay = `${i * 70}ms`;
card.setAttribute('tabindex', '0');
card.setAttribute('aria-label', `${product.name}, Price: ${formatPrice(product.price)}`);
const img = document.createElement('img');
img.src = product.image;
img.alt = product.alt;
const name = document.createElement('h3');
name.className = 'product-name';
name.textContent = product.name;
const price = document.createElement('p');
price.className = 'product-price';
price.textContent = formatPrice(product.price);
const btnAdd = document.createElement('button');
btnAdd.className = 'add-cart-btn';
btnAdd.textContent = 'Add to Cart';
btnAdd.setAttribute('aria-label', `Add ${product.name} to cart`);
btnAdd.addEventListener('click', (e) => {
e.stopPropagation();
addToCart(product.id);
});
card.appendChild(img);
card.appendChild(name);
card.appendChild(price);
card.appendChild(btnAdd);
productsContainer.appendChild(card);
});
}
function renderCart() {
cartItemsContainer.innerHTML = '';
const cartKeys = Object.keys(cart);
if(cartKeys.length === 0) {
const empty = document.createElement('p');
empty.className = 'cart-empty';
empty.textContent = 'Your cart is empty';
empty.setAttribute('aria-live', 'assertive');
cartItemsContainer.appendChild(empty);
cartTotalDisplay.textContent = `Total: $0.00`;
btnCheckoutPanel.disabled = true;
navCartCount.textContent = '0';
return;
}
let total = 0;
cartKeys.forEach((pid) => {
const item = cart[pid];
const product = products.find(p => p.id === pid);
if(!product) return; // safety
const itemTotal = product.price * item.quantity;
total += itemTotal;
const cartItem = document.createElement('div');
cartItem.className = 'cart-item';
const img = document.createElement('img');
img.src = product.image;
img.alt = product.alt;
const info = document.createElement('div');
info.className = 'cart-item-info';
const name = document.createElement('p');
name.className = 'cart-item-name';
name.textContent = product.name;
const priceQty = document.createElement('div');
priceQty.className = 'cart-item-price-qty';
const priceEl = document.createElement('span');
priceEl.className = 'cart-item-price';
priceEl.textContent = formatPrice(itemTotal);
const qtyControls = document.createElement('span');
qtyControls.className = 'cart-item-controls';
const btnDec = document.createElement('button');
btnDec.className = 'cart-btn';
btnDec.setAttribute('aria-label', `Decrease quantity of ${product.name}`);
btnDec.textContent = '-';
btnDec.addEventListener('click', () => {
updateQuantity(pid, item.quantity - 1);
});
const qtyDisplay = document.createElement('span');
qtyDisplay.setAttribute('aria-live', 'polite');
qtyDisplay.setAttribute('aria-atomic', 'true');
qtyDisplay.style.minWidth = '22px';
qtyDisplay.style.display = 'inline-block';
qtyDisplay.style.textAlign = 'center';
qtyDisplay.textContent = item.quantity;
const btnInc = document.createElement('button');
btnInc.className = 'cart-btn';
btnInc.setAttribute('aria-label', `Increase quantity of ${product.name}`);
btnInc.textContent = '+';
btnInc.addEventListener('click', () => {
updateQuantity(pid, item.quantity + 1);
});
qtyControls.appendChild(btnDec);
qtyControls.appendChild(qtyDisplay);
qtyControls.appendChild(btnInc);
priceQty.appendChild(priceEl);
priceQty.appendChild(qtyControls);
info.appendChild(name);
info.appendChild(priceQty);
cartItem.appendChild(img);
cartItem.appendChild(info);
cartItemsContainer.appendChild(cartItem);
});
cartTotalDisplay.textContent = `Total: ${formatPrice(total)}`;
btnCheckoutPanel.disabled = false;
navCartCount.textContent = cartKeys.reduce((acc, pid) => acc + cart[pid].quantity, 0);
}
function addToCart(productId) {
if(cart[productId]) {
cart[productId].quantity++;
} else {
cart[productId] = { quantity: 1 };
}
saveCart();
renderCart();
animateAddToCart(productId);
}
function updateQuantity(productId, qty) {
if(qty <= 0) {
delete cart[productId];
} else {
cart[productId].quantity = qty;
}
saveCart();
renderCart();
}
// Animating product cards when added
function animateAddToCart(productId) {
const productsEls = [...productsContainer.querySelectorAll('.product-card')];
const productIndex = products.findIndex(p => p.id === productId);
if(productIndex < 0) return;
const productEl = productsEls[productIndex];
if(!productEl) return;
productEl.style.transition = 'transform 0.15s ease';
productEl.style.transform = 'scale(1.15)';
productEl.style.boxShadow = '0 0 30px #00b0ffee';
setTimeout(() => {
productEl.style.transform = 'scale(1)';
productEl.style.boxShadow = '';
}, 150);
}
// Category filtering
categoryButtons.forEach(btn => {
btn.addEventListener('click', () => {
if(btn.classList.contains('active')) return;
categoryButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
renderProducts(btn.dataset.category);
productsContainer.focus();
});
});
// Checkout modal logic
const openModal = () => {
if(Object.keys(cart).length === 0) {
alert('Your cart is empty! Add some products before checking out.');
return;
}
deliveryStatus.style.display = 'none';
checkoutForm.style.display = 'flex';
checkoutForm.reset();
checkoutModal.setAttribute('aria-hidden', 'false');
checkoutModal.classList.add('active');
checkoutForm.querySelector('input,select').focus();
};
const closeModal = () => {
checkoutModal.classList.remove('active');
checkoutModal.setAttribute('aria-hidden', 'true');
deliveryStatus.style.display = 'none';
checkoutForm.style.display = 'flex';
};
modalCloseBtn.addEventListener('click', closeModal);
checkoutModal.addEventListener('click', e => {
if(e.target === checkoutModal) {
closeModal();
}
});
// Keyboard accessibility for modal - trap focus & ESC close
let lastFocusedElementBeforeModal;
function trapFocusInModal(e) {
if (!checkoutModal.classList.contains('active')) return;
const focusableElements = checkoutModal.querySelectorAll('button, input, select');
const firstEl = focusableElements[0];
const lastEl = focusableElements[focusableElements.length - 1];
if (e.key === 'Tab') {
if (e.shiftKey) { // Shift+Tab
if (document.activeElement === firstEl) {
e.preventDefault();
lastEl.focus();
}
} else { // Tab
if (document.activeElement === lastEl) {
e.preventDefault();
firstEl.focus();
}
}
}
if(e.key === 'Escape') {
closeModal();
if(lastFocusedElementBeforeModal){
lastFocusedElementBeforeModal.focus();
}
}
}
document.addEventListener('keydown', trapFocusInModal);
// Checkout button open modal
btnCheckoutPanel.addEventListener('click', () => {
lastFocusedElementBeforeModal = document.activeElement;
openModal();
});
document.getElementById('nav-checkout').addEventListener('click', () => {
lastFocusedElementBeforeModal = document.activeElement;
openModal();
});
// Checkout form submit - simulate delivery progress
checkoutForm.addEventListener('submit', (e) => {
e.preventDefault();
if(!checkoutForm.checkValidity()){
checkoutForm.reportValidity();
return;
}
// Disable form
const inputs = checkoutForm.querySelectorAll('input, select, button');
inputs.forEach(i => i.disabled = true);
// Show delivery status area
deliveryStatus.style.display = 'block';
deliveryStatus.textContent = 'Order is being processed...';
// Simulate delivery progress with steps and animations + timers
let step = 0;
const steps = [
'Order confirmed. Preparing your items...',
'Packing your products carefully...',
'Handing over to delivery partner...',
'Your order is out for delivery!',
'Delivery completed! Thank you for shopping with us.',
];
deliveryStatus.style.boxShadow = '0 0 15px #39aaffbb, 0 0 30px #00a0ff88 inset';
deliveryStatus.style.animation = 'pulseGlow 3s infinite ease-in-out';
const nextStep = () => {
if(step >= steps.length){
// Finished
deliveryStatus.style.animation = '';
deliveryStatus.style.boxShadow = '';
// Clear cart
cart = {};
saveCart();
renderCart();
alert('🎉 Your order has been successfully placed! Thank you! 🎉');
closeModal();
return;
}
deliveryStatus.textContent = `${steps[step]} (${step + 1}/${steps.length})`;
step++;
setTimeout(nextStep, 3500);
};
setTimeout(nextStep, 1000);
});
// Navigation buttons: Home scrolls back to all products
document.getElementById('nav-home').addEventListener('click', () => {
categoryButtons.forEach(b => b.classList.remove('active'));
const allBtn = [...categoryButtons].find(b => b.dataset.category === 'all');
if(allBtn) allBtn.classList.add('active');
renderProducts('all');
productsContainer.focus();
});
// Navigation cart button: just focus cart panel
document.getElementById('nav-cart').addEventListener('click', () => {
document.getElementById('cart-panel').focus();
});
// Initialize app render
renderProducts();
renderCart();
})();
</script>
</body>
</html>