写一个静态轮播图的html代码
时间: 2023-12-25 19:21:02 浏览: 70
好的,以下是一个简单的静态轮播图的HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>静态轮播图</title>
<style>
.container {
width: 500px;
height: 300px;
overflow: hidden;
position: relative;
}
.slide {
position: absolute;
top: 0;
left: 0;
width: 500px;
height: 300px;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slide.active {
opacity: 1;
transition: opacity 1s ease-in-out;
}
.slide:first-child {
opacity: 1;
}
.nav {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.nav button {
width: 10px;
height: 10px;
border-radius: 50%;
margin: 0 5px;
background-color: #ccc;
border: none;
cursor: pointer;
}
.nav button.active {
background-color: #333;
}
</style>
</head>
<body>
<div class="container">
<div class="slide active">
<img src="slide1.jpg" alt="Slide 1">
</div>
<div class="slide">
<img src="slide2.jpg" alt="Slide 2">
</div>
<div class="slide">
<img src="slide3.jpg" alt="Slide 3">
</div>
<div class="slide">
<img src="slide4.jpg" alt="Slide 4">
</div>
<div class="nav">
<button class="active"></button>
<button></button>
<button></button>
<button></button>
</div>
</div>
<script>
let slides = document.querySelectorAll('.slide');
let buttons = document.querySelectorAll('.nav button');
let currentSlide = 0;
function nextSlide() {
slides[currentSlide].classList.remove('active');
buttons[currentSlide].classList.remove('active');
currentSlide = (currentSlide + 1) % slides.length;
slides[currentSlide].classList.add('active');
buttons[currentSlide].classList.add('active');
}
let slideInterval = setInterval(nextSlide, 5000);
</script>
</body>
</html>
```
这个代码中包含一个容器 `.container` 和多个轮播项 `.slide`,以及一个导航栏 `.nav`。轮播项使用 CSS 的 `position: absolute` 属性定位,导航栏使用 flexbox 布局,并且通过 JavaScript 实现轮播的效果。
阅读全文