写一个css+html+js 的高级网页,网页内容美食
时间: 2024-03-05 14:52:56 浏览: 71
基于HTML+CSS+JS的美食网站设计
5星 · 资源好评率100%
好的,以下是一个简单的美食网页的 HTML、CSS、JavaScript 代码示例,实现了轮播图、鼠标悬停效果、弹出框等功能。
HTML 代码:
```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>美食网站</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">热门美食</a></li>
<li><a href="#">最新菜谱</a></li>
<li><a href="#">美食视频</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</nav>
</header>
<main>
<div class="slideshow">
<img src="food1.jpg" alt="美食1">
<img src="food2.jpg" alt="美食2">
<img src="food3.jpg" alt="美食3">
<img src="food4.jpg" alt="美食4">
</div>
<h1>热门美食推荐</h1>
<ul class="food-list">
<li>
<img src="food5.jpg" alt="美食5">
<h3>美食5</h3>
<p>描述美食5的信息。</p>
<button class="btn">查看详情</button>
</li>
<li>
<img src="food6.jpg" alt="美食6">
<h3>美食6</h3>
<p>描述美食6的信息。</p>
<button class="btn">查看详情</button>
</li>
<li>
<img src="food7.jpg" alt="美食7">
<h3>美食7</h3>
<p>描述美食7的信息。</p>
<button class="btn">查看详情</button>
</li>
<li>
<img src="food8.jpg" alt="美食8">
<h3>美食8</h3>
<p>描述美食8的信息。</p>
<button class="btn">查看详情</button>
</li>
</ul>
</main>
<footer>
<p>版权所有 © 美食网站</p>
</footer>
<div class="overlay">
<div class="modal">
<h2>美食详情</h2>
<img src="food9.jpg" alt="美食9">
<p>描述美食9的信息。</p>
<button class="close-btn">×</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
/* Reset styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Header styles */
header {
background-color: #333;
color: #fff;
padding: 20px;
}
nav ul {
list-style: none;
display: flex;
}
nav li {
margin-right: 20px;
}
nav a {
color: #fff;
text-decoration: none;
}
/* Main styles */
.slideshow {
width: 100%;
height: 500px;
overflow: hidden;
position: relative;
}
.slideshow img {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slideshow img.active {
opacity: 1;
}
h1 {
text-align: center;
margin: 50px 0;
}
.food-list {
list-style: none;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.food-list li {
margin: 30px;
position: relative;
width: 300px;
height: 400px;
}
.food-list li:hover .btn {
bottom: 20px;
}
.food-list img {
width: 100%;
height: 300px;
object-fit: cover;
}
.food-list h3 {
margin: 20px 0;
}
.btn {
position: absolute;
bottom: -50px;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
transition: bottom 0.5s ease-in-out;
}
/* Footer styles */
footer {
background-color: #333;
color: #fff;
padding: 20px;
margin-top: 50px;
text-align: center;
}
/* Overlay styles */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
}
.modal {
width: 500px;
background-color: #fff;
padding: 20px;
text-align: center;
position: relative;
}
.modal img {
width: 100%;
height: 300px;
object-fit: cover;
}
.modal h2 {
margin-bottom: 20px;
}
.close-btn {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
border: none;
font-size: 30px;
cursor: pointer;
}
```
JavaScript 代码:
```javascript
// Get slideshow images
const slideshow = document.querySelector('.slideshow');
const images = slideshow.querySelectorAll('img');
let currentIndex = 0;
// Set active class to first image
images[currentIndex].classList.add('active');
// Change image every 3 seconds
setInterval(() => {
images[currentIndex].classList.remove('active');
currentIndex = (currentIndex + 1) % images.length;
images[currentIndex].classList.add('active');
}, 3000);
// Show modal when button is clicked
const overlay = document.querySelector('.overlay');
const modal = document.querySelector('.modal');
const closeBtn = modal.querySelector('.close-btn');
const buttons = document.querySelectorAll('.btn');
buttons.forEach((button) => {
button.addEventListener('click', () => {
overlay.style.display = 'flex';
modal.style.display = 'block';
});
});
// Hide modal when close button is clicked
closeBtn.addEventListener('click', () => {
overlay.style.display = 'none';
modal.style.display = 'none';
});
// Hide modal when overlay is clicked
overlay.addEventListener('click', (event) => {
if (event.target === overlay) {
overlay.style.display = 'none';
modal.style.display = 'none';
}
});
```
请注意,这只是一个简单的示例代码,实际网页需要根据需求进行优化和完善,比如添加更多的美食信息、优化页面布局等等。
阅读全文