用html,css,js写一个蔬菜的购买页面
时间: 2023-12-17 11:06:10 浏览: 171
基于HTML/CSS/JavaScript的蔬菜商城网页设计源码——每日新鲜蔬菜商城网站搭建
以下是一个简单的蔬菜购买页面的示例,使用了HTML、CSS和JavaScript:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-">
<title>蔬菜购买页面</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>欢迎来到蔬菜购买页面</h1>
<div class="container">
<div class="vegetables">
<h2>蔬菜列表</h2>
<ul>
<li>
<img src="https://cdn.pixabay.com/photo/2016/04/01/09/35/carrot-1300250_960_720.png" alt="胡萝卜">
<h3>胡萝卜</h3>
<p class="price">¥3</p>
<button class="buy-btn">加入购物车</button>
</li>
<li>
<img src="https://cdn.pixabay.com/photo/2017/06/21/15/11/tomatoes-2428944_960_720.png" alt="番茄">
<h3>番茄</h3>
<p class="price">¥4</p>
<button class="buy-btn">加入购物车</button>
</li>
<li>
<img src="https://cdn.pixabay.com/photo/2016/04/01/08/46/broccoli-1299624_960_720.png" alt="西兰花">
<h3>西兰花</h3>
<p class="price">¥5</p>
<button class="buy-btn">加入购物车</button>
</li>
</ul>
</div>
<div class="cart">
<h2>购物车</h2>
<ul id="cart-list">
</ul>
<p class="total">总价:<span id="total">¥0</span></p>
<button class="checkout-btn">结算</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
h1, h2, h3 {
text-align: center;
margin-bottom: 20px;
}
.container {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.vegetables {
width: 60%;
}
ul {
list-style: none;
}
li {
margin-bottom: 20px;
display: flex;
align-items: center;
}
img {
width: 100px;
height: 100px;
margin-right: 20px;
}
.price {
font-size: 18px;
font-weight: bold;
margin-top: 10px;
margin-bottom: 10px;
}
.buy-btn {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 14px;
margin-top: 10px;
cursor: pointer;
}
.cart {
width: 35%;
}
.total {
font-size: 20px;
font-weight: bold;
margin-top: 20px;
}
.checkout-btn {
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;
}
```
JavaScript代码:
```js
// 获取DOM元素
const cartList = document.getElementById('cart-list');
const total = document.getElementById('total');
const buyBtns = document.querySelectorAll('.buy-btn');
const checkoutBtn = document.querySelector('.checkout-btn');
// 初始化购物车
let cart = [];
// 加入购物车
function addToCart(name, price) {
// 查找购物车中是否已有该商品
const found = cart.find(item => item.name === name);
if (found) {
// 如果已有该商品,则数量加1
found.quantity++;
} else {
// 如果没有该商品,则添加到购物车中
cart.push({ name, price, quantity: 1 });
}
// 更新购物车列表和总价
renderCart();
renderTotal();
}
// 渲染购物车列表
function renderCart() {
cartList.innerHTML = cart.map(item => `
<li>${item.name} × ${item.quantity}:<span class="price">¥${item.price * item.quantity}</span></li>
`).join('');
}
// 渲染总价
function renderTotal() {
const totalPrice = cart.reduce((total, item) => total + item.price * item.quantity, 0);
total.innerHTML = `¥${totalPrice}`;
}
// 点击“加入购物车”按钮时,加入到购物车中
buyBtns.forEach(btn => {
btn.addEventListener('click', e => {
const li = e.target.parentNode;
const name = li.querySelector('h3').textContent;
const price = parseFloat(li.querySelector('.price').textContent.slice(1));
addToCart(name, price);
});
});
// 点击“结算”按钮时,弹出提示框
checkoutBtn.addEventListener('click', () => {
if (cart.length > 0) {
alert(`您已成功购买以下商品:\n${cart.map(item => `${item.name} × ${item.quantity}`).join('\n')}\n总价:${total.textContent}`);
// 清空购物车
cart = [];
// 更新购物车列表和总价
renderCart();
renderTotal();
} else {
alert('购物车为空,请先购买商品!');
}
});
```
该页面展示了三种蔬菜,每种蔬菜都包含名称、价格和“加入购物车”按钮。当用户点击“加入购物车”按钮时,该蔬菜将会添加到购物车中。购物车中展示了已添加的商品列表和总价,用户可以点击“结算”按钮完成购买,弹出提示框显示购买的商品和总价,并清空购物车。
阅读全文