给我一个带导航下拉的导航栏和带小点的轮播图的页面代码
时间: 2024-03-23 09:39:33 浏览: 57
js带导航菜单Banner焦点图轮播代码.zip
以下是一个带导航下拉的导航栏和带小点的轮播图的页面代码示例:
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<title>导航栏和轮播图</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">首页</a></li>
<li><a href="#">关于我们</a>
<ul>
<li><a href="#">公司简介</a></li>
<li><a href="#">团队介绍</a></li>
</ul>
</li>
<li><a href="#">产品中心</a>
<ul>
<li><a href="#">产品一</a></li>
<li><a href="#">产品二</a></li>
<li><a href="#">产品三</a></li>
</ul>
</li>
<li><a href="#">联系我们</a></li>
</ul>
</nav>
</header>
<main>
<div class="slider">
<div class="slide active">
<img src="https://picsum.photos/id/1015/1200/600" alt="Slide 1">
<div class="caption">
<h2>第一张图片标题</h2>
<p>第一张图片描述</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/id/1016/1200/600" alt="Slide 2">
<div class="caption">
<h2>第二张图片标题</h2>
<p>第二张图片描述</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/id/1018/1200/600" alt="Slide 3">
<div class="caption">
<h2>第三张图片标题</h2>
<p>第三张图片描述</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/id/1019/1200/600" alt="Slide 4">
<div class="caption">
<h2>第四张图片标题</h2>
<p>第四张图片描述</p>
</div>
</div>
<div class="slide">
<img src="https://picsum.photos/id/1020/1200/600" alt="Slide 5">
<div class="caption">
<h2>第五张图片标题</h2>
<p>第五张图片描述</p>
</div>
</div>
<div class="navigation">
<div class="dots"></div>
</div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```
header {
background-color: #333;
color: #fff;
padding: 10px;
position: fixed;
top: 0;
left: 0;
width: 100%;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline-block;
position: relative;
}
nav ul li a {
color: #fff;
display: block;
padding: 10px 20px;
text-decoration: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background-color: #333;
padding: 0;
}
nav ul ul li {
display: block;
width: 200px;
}
nav ul ul li a {
padding: 10px;
}
.slider {
position: relative;
height: 600px;
overflow: hidden;
}
.slide {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 0.5s ease-in-out;
}
.slide.active {
opacity: 1;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.caption {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background-color: rgba(0, 0, 0, 0.5);
padding: 20px;
box-sizing: border-box;
color: #fff;
}
.caption h2 {
margin: 0;
font-size: 36px;
font-weight: 600;
}
.caption p {
margin: 10px 0 0;
font-size: 18px;
font-weight: 400;
}
.navigation {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.dots {
display: flex;
justify-content: center;
align-items: center;
}
.dot {
width: 15px;
height: 15px;
border-radius: 50%;
border: 2px solid #fff;
margin: 0 5px;
cursor: pointer;
transition: background-color 0.5s ease-in-out;
}
.dot.active {
background-color: #fff;
}
```
JavaScript代码:
```
const slides = document.querySelectorAll('.slide');
const dots = document.querySelectorAll('.dot');
let currentSlide = 0;
let slideInterval = setInterval(nextSlide, 5000);
function nextSlide() {
slides[currentSlide].classList.remove('active');
dots[currentSlide].classList.remove('active');
currentSlide = (currentSlide + 1) % slides.length;
slides[currentSlide].classList.add('active');
dots[currentSlide].classList.add('active');
}
dots.forEach((dot, index) => {
dot.addEventListener('click', () => {
slides[currentSlide].classList.remove('active');
dots[currentSlide].classList.remove('active');
currentSlide = index;
slides[currentSlide].classList.add('active');
dots[currentSlide].classList.add('active');
clearInterval(slideInterval);
slideInterval = setInterval(nextSlide, 5000);
});
});
```
以上代码实现了一个带导航下拉的导航栏和带小点的轮播图效果,可以根据实际需要进行修改和调整。
阅读全文