商品展示页面,html,css,javascript
时间: 2023-08-22 08:08:57 浏览: 145
好的,以下是商品展示页面的HTML、CSS和JavaScript代码示例:
HTML代码:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Product Page</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Product Page</h1>
</header>
<main>
<div class="products">
<div class="product">
<div class="product-image">
<img src="product-image-1.jpg" alt="Product Image">
</div>
<div class="product-details">
<h2 class="product-title">Product Name 1</h2>
<p class="product-description">Product Description</p>
<p class="product-price">$19.99</p>
<button class="add-to-cart" data-product-id="1">Add to Cart</button>
</div>
</div>
<div class="product">
<div class="product-image">
<img src="product-image-2.jpg" alt="Product Image">
</div>
<div class="product-details">
<h2 class="product-title">Product Name 2</h2>
<p class="product-description">Product Description</p>
<p class="product-price">$29.99</p>
<button class="add-to-cart" data-product-id="2">Add to Cart</button>
</div>
</div>
<div class="product">
<div class="product-image">
<img src="product-image-3.jpg" alt="Product Image">
</div>
<div class="product-details">
<h2 class="product-title">Product Name 3</h2>
<p class="product-description">Product Description</p>
<p class="product-price">$39.99</p>
<button class="add-to-cart" data-product-id="3">Add to Cart</button>
</div>
</div>
</div>
</main>
<footer>
<p>© 2021 Product Page. All rights reserved.</p>
</footer>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
}
header {
background-color: #1abc9c;
color: #fff;
padding: 20px;
text-align: center;
}
main {
padding: 20px;
}
.products {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.product {
flex-basis: calc(33.33% - 20px);
margin-bottom: 20px;
display: flex;
align-items: stretch;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0px 0px 5px #ccc;
}
.product-image {
flex-basis: 40%;
display: flex;
align-items: center;
justify-content: center;
}
.product-image img {
max-width: 100%;
max-height: 100%;
}
.product-details {
flex-basis: 60%;
padding: 20px;
}
.product-title {
font-size: 24px;
margin-bottom: 10px;
}
.product-description {
font-size: 16px;
margin-bottom: 10px;
}
.product-price {
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
}
.add-to-cart {
padding: 10px 20px;
font-size: 16px;
font-weight: bold;
color: #fff;
background-color: #1abc9c;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
}
.add-to-cart:hover {
background-color: #16a085;
}
```
JavaScript代码:
```
const cart = [];
function addToCart(event) {
const productId = event.target.getAttribute('data-product-id');
cart.push(productId);
console.log(cart);
}
const addToCartButtons = document.querySelectorAll('.add-to-cart');
for (let i = 0; i < addToCartButtons.length; i++) {
addToCartButtons[i].addEventListener('click', addToCart);
}
```
以上代码实现了一个简单的商品展示页面,包含商品的名称、描述、价格和添加到购物车的按钮。JavaScript代码实现了将商品添加到购物车的功能,每次单击“Add to Cart”按钮时,将商品ID添加到cart数组中,并在控制台中打印出cart数组。你可以根据需要自定义商品展示页面的样式和功能。
阅读全文