购物系统 商品详情页 html,css,javascript
时间: 2023-07-26 09:03:39 浏览: 367
以下是一个简单的商品详情页的HTML、CSS和JavaScript代码示例:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>商品详情页</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>商品名称</h1>
<div class="product">
<img src="product.jpg" alt="商品图片">
<div class="product-details">
<h2>商品描述</h2>
<p>价格: <span class="price">100</span>元</p>
<p>库存: <span class="inventory">100</span>件</p>
<p>颜色:
<select class="color">
<option value="red">红色</option>
<option value="blue">蓝色</option>
<option value="green">绿色</option>
</select>
</p>
<button class="add-to-cart">加入购物车</button>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
* {
box-sizing: border-box;
font-family: Arial, sans-serif;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
.product {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.product img {
max-width: 50%;
}
.product-details {
flex-basis: 40%;
padding: 20px;
border: 1px solid #ccc;
}
.product-details h2 {
margin-top: 0;
font-size: 1.2em;
}
.product-details p {
margin: 0;
font-size: 0.8em;
color: #666;
}
.product-details select {
margin-left: 10px;
}
.add-to-cart {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin-top: 20px;
cursor: pointer;
}
.add-to-cart:hover {
background-color: #3e8e41;
}
```
JavaScript代码:
```
const priceElement = document.querySelector(".price");
const inventoryElement = document.querySelector(".inventory");
const colorElement = document.querySelector(".color");
const addToCartButton = document.querySelector(".add-to-cart");
// 假设有以下商品数据
const productData = {
name: "商品名称",
price: 100,
inventory: 100,
colors: ["red", "blue", "green"]
};
// 根据商品数据更新页面
function updateProductDetails() {
document.title = productData.name + " - 商品详情页";
document.querySelector("h1").textContent = productData.name;
priceElement.textContent = productData.price;
inventoryElement.textContent = productData.inventory;
colorElement.innerHTML = "";
productData.colors.forEach(color => {
const option = document.createElement("option");
option.value = color;
option.textContent = color;
colorElement.appendChild(option);
});
}
// 加入购物车的事件处理程序
function addToCart() {
// 这里可以编写将商品加入购物车的逻辑
alert("商品已加入购物车!");
}
// 页面加载后
window.addEventListener("load", () => {
updateProductDetails(); // 更新页面
addToCartButton.addEventListener("click", addToCart); // 绑定加入购物车的事件处理程序
});
```
这个示例页面使用了Flexbox布局,并通过JavaScript动态更新商品详情。你可以根据你的需求进行修改和扩展。
阅读全文