设计一个卖衣服的商铺,有价格、衣服、折扣、价格,用英文,html+css
时间: 2023-05-30 22:05:17 浏览: 74
和JavaScript实现。
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>Clothing Store</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>Clothing Store</h1>
</header>
<main>
<section>
<h2>Products</h2>
<ul id="product-list">
<li class="product">
<img src="https://via.placeholder.com/150x150" alt="Product Image">
<h3>Product 1</h3>
<p class="price">$50</p>
</li>
<li class="product">
<img src="https://via.placeholder.com/150x150" alt="Product Image">
<h3>Product 2</h3>
<p class="price">$75</p>
</li>
<li class="product">
<img src="https://via.placeholder.com/150x150" alt="Product Image">
<h3>Product 3</h3>
<p class="price">$100</p>
</li>
</ul>
</section>
<section>
<h2>Discounts</h2>
<ul id="discount-list">
<li class="discount">
<h3>10% off on all products</h3>
<p class="discount-price">$0</p>
</li>
<li class="discount">
<h3>20% off on orders above $100</h3>
<p class="discount-price">$0</p>
</li>
<li class="discount">
<h3>30% off on orders above $200</h3>
<p class="discount-price">$0</p>
</li>
</ul>
</section>
</main>
<footer>
<p>© 2021 Clothing Store</p>
</footer>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
main {
display: flex;
flex-wrap: wrap;
justify-content: center;
padding: 20px;
}
section {
margin: 20px;
}
h2 {
font-size: 24px;
margin-bottom: 10px;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
.product {
background-color: #f2f2f2;
border: 1px solid #ccc;
padding: 20px;
text-align: center;
width: 200px;
}
.product img {
margin-bottom: 20px;
}
.product h3 {
font-size: 18px;
margin-bottom: 10px;
}
.product .price {
font-size: 16px;
font-weight: bold;
margin: 0;
}
.discount {
background-color: #f2f2f2;
border: 1px solid #ccc;
padding: 20px;
text-align: center;
width: 200px;
}
.discount h3 {
font-size: 18px;
margin-bottom: 10px;
}
.discount .discount-price {
font-size: 16px;
font-weight: bold;
margin: 0;
}
footer {
background-color: #333;
color: #fff;
padding: 20px;
text-align: center;
}
```
JavaScript代码:
```
const productPrices = [50, 75, 100]; // Prices of the products
const discountRates = [0.1, 0.2, 0.3]; // Discount rates for different discount levels
const discountThresholds = [0, 100, 200]; // Thresholds for different discount levels
// Function to calculate discounted price
function calculateDiscountedPrice(price, discountRate) {
return price * (1 - discountRate);
}
// Function to update discount prices
function updateDiscountPrices() {
const productList = document.querySelectorAll('.product');
const discountList = document.querySelectorAll('.discount');
const totalPrices = [0, 0, 0];
// Calculate total prices
for (let i = 0; i < productList.length; i++) {
const price = parseInt(productList[i].querySelector('.price').textContent.slice(1));
totalPrices[i] = price;
}
// Apply discounts
for (let i = 0; i < discountList.length; i++) {
const discountRate = discountRates[i];
const discountThreshold = discountThresholds[i];
let totalPrice = 0;
for (let j = 0; j < productList.length; j++) {
if (totalPrices[j] >= discountThreshold) {
totalPrice += calculateDiscountedPrice(totalPrices[j], discountRate);
} else {
totalPrice += totalPrices[j];
}
}
discountList[i].querySelector('.discount-price').textContent = '$' + totalPrice.toFixed(2);
}
}
// Update discount prices when page loads
updateDiscountPrices();
// Update discount prices when product prices change
const productList = document.querySelectorAll('.product');
for (let i = 0; i < productList.length; i++) {
productList[i].addEventListener('click', function() {
const price = parseInt(this.querySelector('.price').textContent.slice(1));
if (price === productPrices[i]) {
this.querySelector('.price').textContent = '$' + calculateDiscountedPrice(price, discountRates[0]).toFixed(2);
} else {
this.querySelector('.price').textContent = '$' + productPrices[i];
}
updateDiscountPrices();
});
}
```
这个商铺可以展示三个产品,并提供三个不同的折扣等级,当用户点击产品时,价格会更改,折扣价格也会更新。