使用html、css、JavaScript设计商品列表
时间: 2023-10-24 20:07:57 浏览: 92
好的,下面是一个简单的商品列表设计示例:
HTML部分:
```
<!DOCTYPE html>
<html>
<head>
<title>商品列表</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>商品列表</h1>
<div class="product-list">
<div class="product-item">
<img src="product1.jpg" alt="商品1">
<h3>商品1</h3>
<p class="price">$10.99</p>
<button>加入购物车</button>
</div>
<div class="product-item">
<img src="product2.jpg" alt="商品2">
<h3>商品2</h3>
<p class="price">$12.99</p>
<button>加入购物车</button>
</div>
<div class="product-item">
<img src="product3.jpg" alt="商品3">
<h3>商品3</h3>
<p class="price">$8.99</p>
<button>加入购物车</button>
</div>
</div>
</body>
</html>
```
CSS部分:
```
body {
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
}
.product-list {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.product-item {
margin: 20px;
text-align: center;
}
.product-item img {
width: 200px;
height: 200px;
object-fit: cover;
}
.price {
font-size: 20px;
font-weight: bold;
color: #ff0000;
margin-top: 10px;
margin-bottom: 10px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
```
JavaScript部分(实现加入购物车功能):
```
var buttons = document.getElementsByTagName("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", addToCart);
}
function addToCart() {
alert("成功加入购物车!");
}
```
以上代码实现了一个简单的商品列表,包含了三个商品项,每个商品项包含商品图片、商品名称、商品价格和加入购物车按钮。CSS样式实现了商品列表的布局和样式,JavaScript实现了加入购物车功能。