html➕css仿小米商城购物车
时间: 2024-06-25 16:01:00 浏览: 164
html+css实现仿小米商城主页
创建一个HTML和CSS的简单购物车模拟器,你可以使用基本的HTML结构来构建页面布局,结合CSS来美化外观,使其类似小米商城的购物车。以下是一个基础示例:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小米商城购物车模拟</title>
<style>
body {
font-family: Arial, sans-serif;
}
.cart {
background-color: #f7f7f7;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.cart-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.cart-header h2 {
margin: 0;
}
.cart-items {
list-style-type: none;
padding: 0;
}
.item {
display: flex;
margin-bottom: 15px;
}
.item img {
width: 50px;
height: 50px;
margin-right: 10px;
}
.item-name, .item-price {
font-weight: bold;
}
.cart-footer {
text-align: right;
}
</style>
</head>
<body>
<div class="cart">
<div class="cart-header">
<h2>我的购物车</h2>
<a href="#" class="cart-empty">清空购物车</a>
</div>
<ul class="cart-items">
<!-- 添加商品项 -->
<li class="item">
<img src="product1.jpg" alt="产品1">
<span class="item-name">产品1</span>
<span class="item-price">¥199.00</span>
<button class="remove-item">移除</button>
</li>
<!-- 添加更多商品项 -->
</ul>
<div class="cart-footer">
<p>总计:¥<span id="total-price">0.00</span></p>
</div>
</div>
<script>
// 假设这里有一个数组存储购物车商品数据,需要添加计算总价格的逻辑
const cartItems = [
{ name: '产品1', price: 199 },
{ name: '产品2', price: 299 },
// ...
];
// 在此处编写代码,动态渲染商品并计算总价
function renderCart() {
let totalPrice = 0;
cartItems.forEach(item => {
totalPrice += item.price;
const li = document.createElement('li');
li.classList.add('item');
li.innerHTML = `
<img src="${item.image}" alt="${item.name}">
<span class="item-name">${item.name}</span>
<span class="item-price">¥${item.price}</span>
<button class="remove-item">移除</button>
`;
li.querySelector('.remove-item').addEventListener('click', () => {
// 移除商品逻辑
});
document.querySelector('.cart-items').appendChild(li);
});
document.getElementById('total-price').textContent = totalPrice;
}
renderCart();
</script>
</body>
</html>
```
阅读全文