帮我做一个网页,顶部导航栏固定50px, 下面是4张图片,上下排列,每一张照片的高度都是浏览器高度减去50px, 鼠标滚轮上下波动切换照片
时间: 2024-12-14 22:19:37 浏览: 15
为了创建这样一个网页,我们可以使用HTML、CSS和JavaScript(如果需要添加滚动监听事件)。下面是一个简单的示例代码:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>图片轮播</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden; /* 防止内容超出 */
height: 100vh; /* 设置body高度等于视口高度 */
position: relative; /* 用于定位图片容器 */
}
.navbar {
position: fixed;
top: 0;
width: 100%;
background-color: #f8f9fa;
height: 50px;
line-height: 50px;
text-align: center;
z-index: 100; /* 确保导航在图片之上 */
}
.carousel {
display: flex;
justify-content: space-around;
align-items: stretch;
position: absolute;
top: 50px;
left: 0;
right: 0;
height: calc(100vh - 100px); /* 计算剩余空间 */
overflow-y: auto;
scrollbar-width: none; /* 隐藏默认滚动条 */
}
.carousel::-webkit-scrollbar { /* 针对Webkit内核浏览器隐藏滚动条 */
display: none;
}
.carousel img {
width: 50%; /* 图片宽度占一半 */
max-width: 100%;
height: auto;
cursor: pointer;
}
/* 当鼠标滚动时显示滚动条 */
@media (hover: hover) {
.carousel:hover::-webkit-scrollbar {
display: block;
}
}
/* 滚动切换图片效果 */
.carousel img:hover {
opacity: 0.7;
}
/* 切换按钮样式 */
.carousel button {
display: none;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/* 添加按钮显示条件,例如在屏幕小于600px时 */
@media screen and (max-width: 600px) {
.carousel button {
display: block;
width: 50px;
height: 50px;
background: transparent;
border: none;
outline: none;
color: white;
font-size: 2rem;
cursor: pointer;
transition: all 0.3s ease;
}
}
</style>
</head>
<body>
<nav class="navbar">顶部导航栏内容</nav>
<div class="carousel">
<img src="image1.jpg" alt="图片1">
<img src="image2.jpg" alt="图片2">
<img src="image3.jpg" alt="图片3">
<img src="image4.jpg" alt="图片4">
<button onclick="prevImage()">上一张</button>
<button onclick="nextImage()">下一张</button>
</div>
<script>
function prevImage() {
// 更改当前显示图片索引
let current = document.querySelector('.carousel img.active');
let images = document.querySelectorAll('.carousel img');
images.forEach((img, index) => {
if (index === 0 && !images[images.length - 1].classList.contains('active')) {
img.classList.add('active');
} else if (current.previousElementSibling) {
current.previousElementSibling.classList.add('active');
}
});
}
function nextImage() {
// 更改当前显示图片索引
let current = document.querySelector('.carousel img.active');
let images = document.querySelectorAll('.carousel img');
images.forEach((img, index) => {
if (index === images.length - 1 && !images[0].classList.contains('active')) {
img.classList.add('active');
} else if (current.nextElementSibling) {
current.nextElementSibling.classList.add('active');
}
});
}
</script>
</body>
</html>
```
阅读全文