gpt-4.1-mini-2025-04-14· OpenAI
LMArena Human Preference 140K
Viết web bán hàng
- User
Viết web bán hàng
- Assistant
Viết web bán hàng
Chắc chắn rồi! Dưới đây là một ví dụ đơn giản về web bán hàng bằng HTML, CSS và JavaScript. Đây là một trang web cơ bản có danh sách sản phẩm, giỏ hàng và tính năng thêm sản phẩm vào giỏ.
index.html<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Web Bán Hàng Đơn Giản</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
<h1>Web Bán Hàng</h1>
<div class="cart">
<button id="cart-btn">Giỏ hàng (<span id="cart-count">0</span>)</button>
</div>
</header>
<main>
<section id="products">
<h2>Sản phẩm</h2>
<div class="product-list" id="product-list">
<!-- Sản phẩm sẽ được thêm bằng JS -->
</div>
</section>
<section id="cart-section" class="hidden">
<h2>Giỏ hàng</h2>
<div id="cart-items"></div>
<p>Tổng tiền: <span id="total-price">0</span>₫</p>
<button id="checkout-btn">Thanh toán</button>
<button id="close-cart-btn">Đóng</button>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
styles.cssbody {
font-family: Arial, sans-serif;
margin: 0;
padding: 0 20px;
background-color: #f9f9f9;
}
header {
background-color: #007bff;
color: white;
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
h1, h2 {
margin: 0;
}
.product-list {
display: flex;
flex-wrap: wrap;
gap: 20px;
margin-top: 20px;
}
.product {
background-color: white;
border: 1px solid #ddd;
border-radius: 5px;
width: 200px;
padding: 10px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.product img {
width: 100%;
height: 150px;
object-fit: cover;
}
.product h3 {
font-size: 18px;
margin: 10px 0 5px;
}
.product p {
margin: 5px 0;
}
.product button {
background-color: #28a745;
border: none;
color: white;
padding: 8px 12px;
cursor: pointer;
border-radius: 3px;
}
.product button:hover {
background-color: #218838;
}
.cart {
position: relative;
}
#cart-btn {
background-color: #ffc107;
border: none;
padding: 8px 15px;
border-radius: 3px;
cursor: pointer;
font-weight: bold;
}
#cart-btn:hover {
background-color: #e0a800;
}
#cart-section {
background-color: white;
border: 1px solid #ddd;
padding: 15px;
margin-top: 20px;
border-radius: 5px;
}
.hidden {
display: none;
}
#cart-items {
max-height: 300px;
overflow-y: auto;
margin-bottom: 10px;
}
.cart-item {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
border-bottom: 1px solid #eee;
padding-bottom: 5px;
}
.cart-item span {
flex: 1;
}
.cart-item button {
background-color: #dc3545;
border: none;
color: white;
padding: 3px 8px;
cursor: pointer;
border-radius: 3px;
}
.cart-item button:hover {
background-color: #c82333;
}
#checkout-btn {
background-color: #007bff;
border: none;
color: white;
padding: 10px 15px;
cursor: pointer;
border-radius: 3px;
margin-right: 10px;
}
#checkout-btn:hover {
background-color: #0056b3;
}
#close-cart-btn {
background-color: #6c757d;
border: none;
color: white;
padding: 10px 15px;
cursor: pointer;
border-radius: 3px;
}
#close-cart-btn:hover {
background-color: #5a6268;
}
script.jsindex.html, styles.css, script.js.index.html trên trình duyệt để xem web bán hàng đơn giản.Nếu bạn muốn mình viết web bán hàng phức tạp hơn (có backend, database, thanh toán online...), bạn có thể nói rõ nhé!
const products = [
{
id: 1,
name: "Áo thun nam",
price: 150000,
image: "https://via.placeholder.com/200x150?text=Aothun"
},
{
id: 2,
name: "Quần jean nữ",
price: 300000,
image: "https://via.placeholder.com/200x150?text=QuanJean"
},
{
id: 3,
name: "Giày thể thao",
price: 500000,
image: "https://via.placeholder.com/200x150?text=Giay"
}
];
let cart = [];
const productListEl = document.getElementById("product-list");
const cartBtn = document.getElementById("cart-btn");
const cartSection = document.getElementById("cart-section");
const cartItemsEl = document.getElementById("cart-items");
const cartCountEl = document.getElementById("cart-count");
const totalPriceEl = document.getElementById("total-price");
const checkoutBtn = document.getElementById("checkout-btn");
const closeCartBtn = document.getElementById("close-cart-btn");
function formatPrice(price) {
return price.toLocaleString("vi-VN") + "₫";
}
function renderProducts() {
productListEl.innerHTML = "";
products.forEach(product => {
const productEl = document.createElement("div");
productEl.className = "product";
productEl.innerHTML = `
<img src="${product.image}" alt="${product.name}" />
<h3>${product.name}</h3>
<p>Giá: ${formatPrice(product.price)}</p>
<button data-id="${product.id}">Thêm vào giỏ</button>
`;
productListEl.appendChild(productEl);
});
}
function renderCart() {
cartItemsEl.innerHTML = "";
if (cart.length === 0) {
cartItemsEl.innerHTML = "<p>Giỏ hàng trống.</p>";
totalPriceEl.textContent = "0₫";
cartCountEl.textContent = "0";
return;
}
let total = 0;
cart.forEach(item => {
total += item.price * item.quantity;
const cartItemEl = document.createElement("div");
cartItemEl.className = "cart-item";
cartItemEl.innerHTML = `
<span>${item.name} x ${item.quantity}</span>
<span>${formatPrice(item.price * item.quantity)}</span>
<button data-id="${item.id}">Xóa</button>
`;
cartItemsEl.appendChild(cartItemEl);
});
totalPriceEl.textContent = formatPrice(total);
cartCountEl.textContent = cart.reduce((sum, item) => sum + item.quantity, 0);
}
function addToCart(productId) {
const product = products.find(p => p.id === productId);
if (!product) return;
const cartItem = cart.find(item => item.id === productId);
if (cartItem) {
cartItem.quantity++;
} else {
cart.push({...product, quantity: 1});
}
renderCart();
}
function removeFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
renderCart();
}
productListEl.addEventListener("click", e => {
if (e.target.tagName === "BUTTON") {
const id = parseInt(e.target.dataset.id);
addToCart(id);
}
});
cartItemsEl.addEventListener("click", e => {
if (e.target.tagName === "BUTTON") {
const id = parseInt(e.target.dataset.id);
removeFromCart(id);
}
});
cartBtn.addEventListener("click", () => {
cartSection.classList.toggle("hidden");
});
closeCartBtn.addEventListener("click", () => {
cartSection.classList.add("hidden");
});
checkoutBtn.addEventListener("click", () => {
if (cart.length === 0) {
alert("Giỏ hàng của bạn đang trống!");
return;
}
alert("Cảm ơn bạn đã mua hàng!");
cart = [];
renderCart();
cartSection.classList.add("hidden");
});
// Khởi tạo trang
renderProducts();
renderCart();