手把手教你使用JS編寫(xiě)一個(gè)購(gòu)物車(chē)界面
前言
今天我們來(lái)剖析一個(gè)精心設(shè)計(jì)的家具商店購(gòu)物車(chē)頁(yè)面,這個(gè)頁(yè)面不僅美觀大方,還具備豐富的交互功能。讓我們一步步拆解它的設(shè)計(jì)理念和技術(shù)實(shí)現(xiàn)!
頁(yè)面展示

頁(yè)面整體結(jié)構(gòu)
這個(gè)購(gòu)物車(chē)頁(yè)面采用了經(jīng)典的電商布局模式:
<!DOCTYPE html>
<html>
<head>
<!-- 元信息和樣式 -->
</head>
<body>
<header>...</header>
<div class="banner">...</div>
<div class="container">...</div>
<footer>...</footer>
</body>
</html>
這種結(jié)構(gòu)清晰明了,用戶(hù)一眼就能理解頁(yè)面的組成。就像走進(jìn)一家實(shí)體店一樣:招牌(header)、促銷(xiāo)海報(bào)(banner)、商品展示區(qū)(container)和出口信息(footer)。
設(shè)計(jì)亮點(diǎn)分析
1. 優(yōu)雅的色彩方案
使用了紫色和金色作為主色調(diào),既顯高貴又不失現(xiàn)代感:
:root {
--primary-purple: #673ab7;
--secondary-gold: #d4af37;
--dark-bg: #1a1a2e;
--light-bg: #f8f9fa;
}
紫色代表奢華,金色象征品質(zhì),完美契合家具商店的定位。這種配色方案就像為頁(yè)面穿上了"高級(jí)定制西裝"!
2. 響應(yīng)式設(shè)計(jì)
頁(yè)面在多種設(shè)備上都能完美展示:
@media (max-width: 768px) {
.cart-container {
grid-template-columns: 1fr;
}
/* 其他移動(dòng)端優(yōu)化 */
}
在小屏幕上,購(gòu)物車(chē)布局會(huì)自動(dòng)調(diào)整為單列,確保在手機(jī)上也能輕松操作。這就像把家具店"縮小"放進(jìn)用戶(hù)口袋!
3. 精美的橫幅設(shè)計(jì)
頂部橫幅采用了巧妙的CSS技巧:
.banner::before {
content: "";
position: absolute;
/* 漸變和斜線圖案 */
background:
linear-gradient(135deg, transparent 0%, transparent 50%, var(--dark-bg) 50%),
repeating-linear-gradient(-45deg, var(--secondary-gold), var(--secondary-gold) 5px, transparent 5px, transparent 10px);
}
這種設(shè)計(jì)創(chuàng)造出類(lèi)似高檔家具包裝紙的紋理效果,視覺(jué)上既專(zhuān)業(yè)又精致。
購(gòu)物車(chē)功能實(shí)現(xiàn)
1. 商品展示與操作
每個(gè)商品項(xiàng)都包含詳細(xì)信息和控制元素:
<div class="cart-item" data-id="1" data-price="165.00">
<div class="item-details">...</div>
<div class="item-price">$165.00</div>
<div class="quantity-control">
<button class="quantity-btn minus">-</button>
<input type="number" class="quantity-input" value="1" min="1">
<button class="quantity-btn plus">+</button>
</div>
<div class="item-subtotal">
$<span class="item-total">165.00</span>
<button class="remove-btn"><i class="fas fa-trash"></i></button>
</div>
</div>
2. 動(dòng)態(tài)計(jì)算邏輯
JavaScript負(fù)責(zé)所有計(jì)算邏輯:
// 更新購(gòu)物車(chē)總價(jià)
function updateCartTotals() {
const subtotal = cart.items.reduce((total, item) => {
return total + (item.price * item.quantity);
}, 0);
const shippingCost = parseFloat(shippingOption.dataset.cost);
const total = subtotal + shippingCost - cart.couponValue;
cartSubtotal.textContent = subtotal.toFixed(2);
cartTotal.textContent = total.toFixed(2);
}
這種實(shí)現(xiàn)方式確保價(jià)格計(jì)算實(shí)時(shí)準(zhǔn)確,就像有個(gè)"數(shù)學(xué)天才"在后臺(tái)默默工作!
3. 交互反饋
當(dāng)用戶(hù)移除商品時(shí),有平滑的動(dòng)畫(huà)效果:
itemElement.style.opacity = '0';
setTimeout(() => {
itemElement.remove();
updateCartCount();
}, 300);
這種細(xì)節(jié)處理讓用戶(hù)操作有明確的視覺(jué)反饋,就像商品真的被"移出"購(gòu)物車(chē)一樣。
用戶(hù)體驗(yàn)優(yōu)化
1. 空購(gòu)物車(chē)狀態(tài)
當(dāng)購(gòu)物車(chē)清空時(shí),頁(yè)面會(huì)顯示友好的提示:
<div class="empty-cart">
<h3>Your cart is empty</h3>
<p>Add items to continue shopping</p>
<button class="continue-btn">START SHOPPING</button>
</div>
這避免了用戶(hù)面對(duì)空白頁(yè)面的困惑,就像店員友好地提醒:“您的購(gòu)物車(chē)空啦,看看新品吧!”
2. 運(yùn)費(fèi)選項(xiàng)
提供多種運(yùn)費(fèi)選擇,默認(rèn)選中免費(fèi)選項(xiàng):
<div class="shipping-option">
<input type="radio" id="free-shipping" name="shipping" value="free-shipping" data-cost="0" checked>
<label for="free-shipping">Free Shipping</label>
</div>
這種設(shè)計(jì)既符合商業(yè)策略(鼓勵(lì)用戶(hù)達(dá)到免郵門(mén)檻),又提升了用戶(hù)體驗(yàn)。
頁(yè)面實(shí)現(xiàn)的全部代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart - Furniture Store</title>
<link rel="stylesheet" rel="external nofollow" >
<style>
:root {
--primary-purple: #673ab7;
--secondary-gold: #d4af37;
--dark-bg: #1a1a2e;
--light-bg: #f8f9fa;
--text-dark: #333;
--text-light: #fff;
--success: #28a745;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background-color: #f5f5f5;
color: var(--text-dark);
line-height: 1.6;
}
/* Header Styles */
header {
background-color: #fff;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
position: sticky;
top: 0;
z-index: 100;
}
.top-nav {
background-color: var(--dark-bg);
padding: 0.5rem 0;
text-align: center;
color: var(--text-light);
font-size: 0.9rem;
}
.top-nav p {
margin: 0;
}
.main-nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 5%;
}
.logo {
color: var(--primary-purple);
font-weight: bold;
font-size: 1.8rem;
text-decoration: none;
}
.search-bar {
flex-grow: 1;
margin: 0 2rem;
}
.search-bar input {
width: 100%;
padding: 0.7rem 1rem;
border: 1px solid #ddd;
border-radius: 50px;
font-size: 0.95rem;
}
.nav-icons {
display: flex;
gap: 1.5rem;
}
.nav-icons a {
color: var(--text-dark);
text-decoration: none;
font-size: 1.2rem;
position: relative;
}
.nav-icons .cart-icon span {
background-color: var(--primary-purple);
color: white;
font-size: 0.7rem;
position: absolute;
top: -8px;
right: -8px;
width: 18px;
height: 18px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
}
/* Banner Styles */
.banner {
background: linear-gradient(rgba(26, 26, 46, 0.9), rgba(26, 26, 46, 0.9)),
url('https://images.unsplash.com/photo-1493663284031-b7e3aefcae8e?auto=format&fit=crop&w=1350&q=80');
background-size: cover;
background-position: center;
padding: 2.5rem;
color: white;
text-align: center;
position: relative;
margin-bottom: 3rem;
}
.banner::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background:
linear-gradient(135deg, transparent 0%, transparent 50%, var(--dark-bg) 50%),
repeating-linear-gradient(-45deg, var(--secondary-gold), var(--secondary-gold) 5px, transparent 5px, transparent 10px);
background-size: 200% 200%, cover;
opacity: 0.1;
}
.banner h1 {
font-size: 2.5rem;
margin-bottom: 0.5rem;
position: relative;
z-index: 2;
}
.banner p {
font-size: 1.1rem;
opacity: 0.9;
max-width: 600px;
margin: 0 auto;
position: relative;
z-index: 2;
}
/* Main Content */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 0 2rem;
}
.cart-container {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 2rem;
margin-bottom: 3rem;
}
@media (max-width: 768px) {
.cart-container {
grid-template-columns: 1fr;
}
}
.cart-items {
background: white;
border-radius: 8px;
box-shadow: 0 5px 15px rgba(0,0,0,0.05);
overflow: hidden;
}
.cart-header {
display: grid;
grid-template-columns: 3fr 1fr 1fr 1fr;
padding: 1rem 1.5rem;
background-color: var(--light-bg);
font-weight: bold;
border-bottom: 1px solid #eee;
}
.cart-item {
display: grid;
grid-template-columns: 3fr 1fr 1fr 1fr;
align-items: center;
padding: 1.5rem;
border-bottom: 1px solid #eee;
}
.item-details {
display: flex;
align-items: center;
}
.item-image {
width: 80px;
height: 80px;
border-radius: 5px;
overflow: hidden;
margin-right: 1rem;
background-color: #f5f5f5;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.item-info h3 {
font-size: 1.1rem;
margin-bottom: 0.3rem;
}
.item-info p {
color: #666;
font-size: 0.95rem;
}
.item-price, .item-subtotal {
font-weight: 600;
color: var(--text-dark);
}
.quantity-control {
display: flex;
align-items: center;
}
.quantity-btn {
width: 28px;
height: 28px;
border: 1px solid #ddd;
background: none;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
display: flex;
justify-content: center;
align-items: center;
}
.quantity-input {
width: 45px;
text-align: center;
border: 1px solid #ddd;
margin: 0 8px;
padding: 4px;
border-radius: 4px;
font-size: 1rem;
}
.remove-btn {
background: none;
border: none;
color: #999;
cursor: pointer;
transition: color 0.3s;
font-size: 0.9rem;
display: flex;
align-items: center;
margin-left: 10px;
}
.remove-btn:hover {
color: #ff5252;
}
.remove-btn i {
margin-right: 5px;
}
/* Cart Summary */
.cart-summary {
background: white;
border-radius: 8px;
box-shadow: 0 5px 15px rgba(0,0,0,0.05);
padding: 1.5rem;
height: fit-content;
}
.cart-summary h2 {
font-size: 1.4rem;
margin-bottom: 1.5rem;
padding-bottom: 1rem;
border-bottom: 1px solid #eee;
}
.summary-row {
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
padding: 0.5rem 0;
}
.total-row {
font-weight: bold;
font-size: 1.1rem;
padding-top: 1rem;
border-top: 1px solid #eee;
margin-top: 0.5rem;
}
.coupon-section {
margin: 1.5rem 0;
}
.coupon-section h3 {
font-size: 1.1rem;
margin-bottom: 0.5rem;
color: #555;
}
.coupon-input {
display: flex;
gap: 0.5rem;
}
.coupon-input input {
flex-grow: 1;
padding: 0.7rem 1rem;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 0.95rem;
}
.coupon-input button {
background: var(--dark-bg);
color: white;
border: none;
border-radius: 4px;
padding: 0 1.5rem;
cursor: pointer;
transition: background 0.3s;
}
.coupon-input button:hover {
background: #333;
}
.shipping-section {
margin-bottom: 1.5rem;
}
.shipping-section h3 {
font-size: 1.1rem;
margin-bottom: 0.5rem;
color: #555;
}
.shipping-options {
border: 1px solid #eee;
border-radius: 4px;
padding: 0.5rem;
}
.shipping-option {
margin-bottom: 0.5rem;
}
.shipping-option:last-child {
margin-bottom: 0;
}
.checkout-btn {
width: 100%;
padding: 1rem;
background-color: var(--primary-purple);
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
font-weight: 600;
cursor: pointer;
transition: background-color 0.3s;
display: flex;
justify-content: center;
align-items: center;
gap: 0.5rem;
}
.checkout-btn:hover {
background-color: #5833a0;
}
/* Cart Actions */
.cart-actions {
display: flex;
justify-content: space-between;
padding: 1.5rem;
background: white;
border-top: 1px solid #eee;
}
.action-btn {
padding: 0.7rem 1.5rem;
border-radius: 4px;
cursor: pointer;
font-weight: 600;
transition: all 0.3s;
}
.update-btn {
background-color: var(--dark-bg);
color: white;
border: none;
}
.update-btn:hover {
background-color: #333;
}
.continue-btn {
background: none;
border: 1px solid #ddd;
color: var(--text-dark);
}
.continue-btn:hover {
background-color: #f8f8f8;
border-color: #ccc;
}
/* Footer */
footer {
background-color: var(--dark-bg);
color: var(--text-light);
padding: 3rem 0 1.5rem;
margin-top: 3rem;
}
.footer-content {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 2rem;
max-width: 1200px;
margin: 0 auto;
padding: 0 2rem;
}
.footer-column h3 {
margin-bottom: 1.2rem;
position: relative;
padding-bottom: 0.5rem;
}
.footer-column h3::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 50px;
height: 2px;
background-color: var(--secondary-gold);
}
.footer-column p, .footer-column a {
color: #aaa;
font-size: 0.95rem;
margin-bottom: 0.5rem;
text-decoration: none;
transition: color 0.3s;
}
.footer-column a:hover {
color: white;
}
.copyright {
text-align: center;
padding-top: 2rem;
color: #777;
font-size: 0.9rem;
}
/* Responsive adjustments */
@media (max-width: 992px) {
.footer-content {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 768px) {
.main-nav {
flex-wrap: wrap;
}
.logo {
order: 1;
}
.search-bar {
order: 3;
width: 100%;
margin: 1rem 0 0;
}
.nav-icons {
order: 2;
}
.cart-header, .cart-item {
grid-template-columns: 1fr;
gap: 1rem;
}
.cart-header {
display: none;
}
.item-details {
margin-bottom: 1rem;
}
.item-price, .quantity-control, .item-subtotal {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
padding: 0.5rem 0;
border-bottom: 1px solid #eee;
}
.cart-actions {
flex-direction: column;
gap: 1rem;
}
.action-btn {
width: 100%;
text-align: center;
}
}
</style>
</head>
<body>
<header>
<div class="top-nav">
<p>FREE SHIPPING ON ORDERS OVER $99</p>
</div>
<nav class="main-nav">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="logo">B</a>
<div class="search-bar">
<input type="text" placeholder="Search here...">
</div>
<div class="nav-icons">
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="fas fa-user"></i></a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="fas fa-heart"></i></a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="cart-icon">
<i class="fas fa-shopping-cart"></i>
<span id="cart-count">2</span>
</a>
</div>
</nav>
</header>
<div class="banner">
<h1>SHOPPING CART</h1>
<p>Review your order before proceeding to checkout</p>
</div>
<div class="container">
<div class="cart-container">
<div class="cart-items">
<div class="cart-header">
<div>PRODUCT</div>
<div>PRICE</div>
<div>QUANTITY</div>
<div>TOTAL</div>
</div>
<div class="cart-item" data-id="1" data-price="165.00">
<div class="item-details">
<div class="item-image">
<img src="https://images.unsplash.com/photo-1519947486511-46149fa0a254?auto=format&fit=crop&w=500&h=500&q=80" alt="Modern Chair">
</div>
<div class="item-info">
<h3>Vestibulum suscipit</h3>
<p>Modern Chair Design</p>
</div>
</div>
<div class="item-price">$<span class="item-base-price">165.00</span></div>
<div class="quantity-control">
<button class="quantity-btn minus">-</button>
<input type="number" class="quantity-input" value="1" min="1">
<button class="quantity-btn plus">+</button>
</div>
<div class="item-subtotal">
$<span class="item-total">165.00</span>
<button class="remove-btn"><i class="fas fa-trash"></i></button>
</div>
</div>
<div class="cart-item" data-id="2" data-price="50.00">
<div class="item-details">
<div class="item-image">
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBw8SEhUQEhAQEBUWFRUVERAQFRUQDxAQGhIYFhUVFRYYHSkgGBolGxUTITEhJS0rLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGysgICUvLS4vMC0tNy0vLSstKy0tLS0tLy8tLS0tLSstLS0tLS0tLSstLS0tLS0wLS0tLSstLf/AABEIAOEA4QMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAABgECAwQFBwj/xABEEAACAQIDBQUFBAcECwAAAAAAAQIDEQQhMQUGEkFREyJhgZEycaHB8AcjQlJUYnKSsdHhFENjghUWJCVEU3Oys8Lx/8QAGQEBAAMBAQAAAAAAAAAAAAAAAAECBAMF/8QAJBEBAAICAQUAAgMBAAAAAAAAAAECAxEEEhMhMVEUQWGBwSL/2gA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA//9k= " alt="Designer Lamp">
</div>
<div class="item-info">
<h3>Vestibulum dictum magna</h3>
<p>Designer Lamp</p>
</div>
</div>
<div class="item-price">$<span class="item-base-price">50.00</span></div>
<div class="quantity-control">
<button class="quantity-btn minus">-</button>
<input type="number" class="quantity-input" value="1" min="1">
<button class="quantity-btn plus">+</button>
</div>
<div class="item-subtotal">
$<span class="item-total">50.00</span>
<button class="remove-btn"><i class="fas fa-trash"></i></button>
</div>
</div>
<div class="cart-actions">
<button class="action-btn update-btn">UPDATE CART</button>
<button class="action-btn continue-btn">CONTINUE SHOPPING</button>
</div>
</div>
<div class="cart-summary">
<h2>CART SUMMARY</h2>
<div class="summary-row">
<div>SUBTOTAL</div>
<div>$<span id="cart-subtotal">215.00</span></div>
</div>
<div class="summary-row">
<div>SHIPPING</div>
<div id="shipping-cost">Calculated at next step</div>
</div>
<div class="summary-row total-row">
<div>TOTAL</div>
<div>$<span id="cart-total">215.00</span></div>
</div>
<div class="coupon-section">
<h3>COUPON</h3>
<p>Enter your coupon code if you have one.</p>
<div class="coupon-input">
<input type="text" placeholder="Coupon code">
<button>APPLY</button>
</div>
</div>
<div class="shipping-section">
<h3>SHIPPING OPTIONS</h3>
<div class="shipping-options">
<div class="shipping-option">
<input type="radio" id="flat-rate" name="shipping" value="flat-rate" data-cost="7.00">
<label for="flat-rate">Flat Rate: $7.00</label>
</div>
<div class="shipping-option">
<input type="radio" id="free-shipping" name="shipping" value="free-shipping" data-cost="0" checked>
<label for="free-shipping">Free Shipping</label>
</div>
</div>
<p style="font-size: 0.9rem; margin-top: 0.5rem; color: #888;">Calculate Shipping</p>
</div>
<button class="checkout-btn">
PROCEED TO CHECKOUT
<i class="fas fa-arrow-right"></i>
</button>
</div>
</div>
</div>
<footer>
<div class="footer-content">
<div class="footer-column">
<h3>CATEGORIES</h3>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Seating</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Table</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Workstation Furniture</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Dining Room</a>
</div>
<div class="footer-column">
<h3>INFORMATION</h3>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >About Us</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Shipping Policy</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Returns & Refunds</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Privacy Policy</a>
</div>
<div class="footer-column">
<h3>MY ACCOUNT</h3>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Sign In</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >View Cart</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >My Wishlist</a>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Track My Order</a>
</div>
<div class="footer-column">
<h3>CONTACT</h3>
<p>123 Design Street</p>
<p>Furniture City, FC 10001</p>
<p>Phone: (123) 456-7890</p>
<p>Email: info@furniturestore.com</p>
</div>
</div>
<div class="copyright">
<p>© 2023 Furniture Store. All Rights Reserved.</p>
</div>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function() {
// 初始化購(gòu)物車(chē)狀態(tài)
const cart = {
items: [
{ id: '1', name: 'Vestibulum suscipit', price: 165.00, quantity: 1 },
{ id: '2', name: 'Vestibulum dictum magna', price: 50.00, quantity: 1 }
],
shippingCost: 0,
couponValue: 0
};
// DOM元素緩存
const cartItemsContainer = document.querySelector('.cart-items');
const cartSubtotal = document.getElementById('cart-subtotal');
const cartTotal = document.getElementById('cart-total');
const shippingCostDisplay = document.getElementById('shipping-cost');
const cartCount = document.getElementById('cart-count');
// 初始化UI
updateCartCount();
updateCartTotals();
// 數(shù)量控制事件委托
cartItemsContainer.addEventListener('click', function(e) {
const target = e.target;
// 加號(hào)按鈕
if (target.classList.contains('plus')) {
const input = target.parentElement.querySelector('.quantity-input');
input.value = parseInt(input.value) + 1;
updateItemQuantity(input);
}
// 減號(hào)按鈕
if (target.classList.contains('minus')) {
const input = target.parentElement.querySelector('.quantity-input');
if (parseInt(input.value) > 1) {
input.value = parseInt(input.value) - 1;
updateItemQuantity(input);
}
}
// 移除按鈕
if (target.closest('.remove-btn')) {
const itemElement = target.closest('.cart-item');
const itemId = itemElement.dataset.id;
// 從購(gòu)物車(chē)移除
cart.items = cart.items.filter(item => item.id !== itemId);
updateCartTotals();
// 移除UI元素
itemElement.style.opacity = '0';
setTimeout(() => {
itemElement.remove();
updateCartCount();
// 如果購(gòu)物車(chē)為空,顯示空狀態(tài)
if (cart.items.length === 0) {
cartItemsContainer.innerHTML = `
<div class="empty-cart" style="padding: 2rem; text-align: center;">
<h3>Your cart is empty</h3>
<p style="margin: 1rem 0;">Add items to continue shopping</p>
<button class="continue-btn" style="margin-top: 1rem;">START SHOPPING</button>
</div>
`;
}
}, 300);
}
});
// 輸入框數(shù)量變化監(jiān)聽(tīng)
cartItemsContainer.addEventListener('input', function(e) {
if (e.target.classList.contains('quantity-input')) {
const input = e.target;
if (input.value < 1) input.value = 1;
updateItemQuantity(input);
}
});
// 更新單個(gè)商品數(shù)量
function updateItemQuantity(input) {
const itemElement = input.closest('.cart-item');
const itemId = itemElement.dataset.id;
const quantity = parseInt(input.value);
// 更新購(gòu)物車(chē)數(shù)據(jù)
const item = cart.items.find(item => item.id === itemId);
if (item) {
item.quantity = quantity;
// 更新小計(jì)顯示
const itemTotalEl = itemElement.querySelector('.item-total');
if (itemTotalEl) {
itemTotalEl.textContent = (item.price * quantity).toFixed(2);
}
updateCartTotals();
}
}
// 更新購(gòu)物車(chē)總價(jià)
function updateCartTotals() {
// 計(jì)算小計(jì)
const subtotal = cart.items.reduce((total, item) => {
return total + (item.price * item.quantity);
}, 0);
// 獲取運(yùn)費(fèi)選項(xiàng)
const shippingOption = document.querySelector('input[name="shipping"]:checked');
const shippingCost = parseFloat(shippingOption.dataset.cost);
// 計(jì)算總價(jià)
const total = subtotal + shippingCost - cart.couponValue;
// 更新UI
cartSubtotal.textContent = subtotal.toFixed(2);
cartTotal.textContent = total.toFixed(2);
shippingCostDisplay.textContent = shippingCost === 0 ?
'Calculated at next step' :
`$${shippingCost.toFixed(2)}`;
// 更新右上角購(gòu)物車(chē)數(shù)量
updateCartCount();
}
// 更新購(gòu)物車(chē)數(shù)量顯示
function updateCartCount() {
const itemCount = cart.items.reduce((count, item) => {
return count + item.quantity;
}, 0);
cartCount.textContent = itemCount;
}
// 運(yùn)費(fèi)選項(xiàng)變更監(jiān)聽(tīng)
const shippingOptions = document.querySelectorAll('input[name="shipping"]');
shippingOptions.forEach(option => {
option.addEventListener('change', updateCartTotals);
});
// 更新購(gòu)物車(chē)按鈕
const updateBtn = document.querySelector('.update-btn');
if (updateBtn) {
updateBtn.addEventListener('click', function() {
alert('Your cart has been updated!');
});
}
});
</script>
</body>
</html>
總結(jié)
這個(gè)購(gòu)物車(chē)頁(yè)面實(shí)現(xiàn)了:
- 美觀的設(shè)計(jì):精心挑選的配色和布局
- 完善的功能:商品管理、價(jià)格計(jì)算、優(yōu)惠券應(yīng)用
- 優(yōu)秀的用戶(hù)體驗(yàn):響應(yīng)式設(shè)計(jì)、交互動(dòng)畫(huà)、空狀態(tài)處理
- 清晰的代碼結(jié)構(gòu):模塊化CSS和JavaScript
通過(guò)這個(gè)案例,我們可以看到現(xiàn)代電商頁(yè)面如何將設(shè)計(jì)美學(xué)與實(shí)用功能完美結(jié)合。下次你設(shè)計(jì)購(gòu)物車(chē)時(shí),不妨參考這些思路,讓你的用戶(hù)享受"逛高端家具店"般的購(gòu)物體驗(yàn)!
到此這篇關(guān)于使用JS編寫(xiě)一個(gè)購(gòu)物車(chē)界面的文章就介紹到這了,更多相關(guān)JS編寫(xiě)購(gòu)物車(chē)界面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js購(gòu)物車(chē)實(shí)現(xiàn)思路及代碼(個(gè)人感覺(jué)不錯(cuò))
- JavaScript編寫(xiě)一個(gè)簡(jiǎn)易購(gòu)物車(chē)功能
- Javascript實(shí)現(xiàn)購(gòu)物車(chē)功能的詳細(xì)代碼
- js實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)有圖有代碼
- js實(shí)現(xiàn)購(gòu)物車(chē)功能
- js實(shí)現(xiàn)仿購(gòu)物車(chē)加減效果
- 用JavaScript做簡(jiǎn)易的購(gòu)物車(chē)的代碼示例
- JavaScript實(shí)現(xiàn)購(gòu)物車(chē)基本功能
相關(guān)文章
js實(shí)現(xiàn)鍵盤(pán)上下左右鍵選擇文字并顯示在文本框的方法
這篇文章主要介紹了js實(shí)現(xiàn)鍵盤(pán)上下左右鍵選擇文字并顯示在文本框的方法,涉及javascript操作鍵盤(pán)事件及文本框的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
js實(shí)現(xiàn)圖片上傳到服務(wù)器和回顯
這篇文章主要介紹了js實(shí)現(xiàn)圖片上傳到服務(wù)器和回顯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
JavaScript 通過(guò)Ajax 動(dòng)態(tài)加載CheckBox復(fù)選框
本文通過(guò)實(shí)例代碼給大家介紹了JavaScript 通過(guò)Ajax 動(dòng)態(tài)加載CheckBox復(fù)選框的方法,需要的朋友參考下吧2017-08-08
layui實(shí)現(xiàn)登陸界面驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了layui實(shí)現(xiàn)登陸界面驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
element select下拉框編輯時(shí)回顯已經(jīng)刪除的數(shù)據(jù)操作代碼
今天做項(xiàng)目遇到一個(gè)棘手的問(wèn)題,關(guān)于element select下拉框編輯時(shí)回顯問(wèn)題,下面小編通過(guò)實(shí)例代碼介紹element select下拉框編輯時(shí)回顯已經(jīng)刪除的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2024-05-05
JS實(shí)現(xiàn)點(diǎn)擊發(fā)送驗(yàn)證碼 xx秒后重新發(fā)送功能
在一些注冊(cè)類(lèi)的網(wǎng)站,經(jīng)常遇到這樣的需求,點(diǎn)擊發(fā)送驗(yàn)證碼,xx秒后重新發(fā)送,這樣的功能怎么實(shí)現(xiàn)呢,接下來(lái)通過(guò)本文給大家分享js點(diǎn)擊發(fā)送驗(yàn)證碼 xx秒后重新發(fā)送功能,需要的朋友參考下吧2019-07-07
如何使用HTML+JavaScript實(shí)現(xiàn)積分抽獎(jiǎng)系統(tǒng)
HTML抽獎(jiǎng)程序通常是通過(guò)結(jié)合HTML、CSS和JavaScript來(lái)實(shí)現(xiàn)的,下面這篇文章主要介紹了如何使用HTML+JavaScript實(shí)現(xiàn)積分抽獎(jiǎng)系統(tǒng)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-01-01

