html网页制作CSS轮播
时间: 2023-12-26 17:27:25 浏览: 85
html网页轮播
以下是一个使用HTML和CSS制作轮播的示例:
```html
<!DOCTYPE html>
<html>
<head>
<style>
.slideshow {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slide {
position: absolute;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slide.active {
opacity: 1;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.slide-text {
position: absolute;
bottom: 20px;
left: 20px;
color: white;
font-size: 24px;
}
.slide-text h1 {
margin: 0;
}
.slide-text p {
margin: 10px 0 0;
}
.slide-nav {
position: absolute;
bottom: 20px;
right: 20px;
}
.slide-nav button {
background-color: transparent;
border: none;
color: white;
font-size: 24px;
margin-left: 10px;
cursor: pointer;
}
.slide-nav button.active {
color: red;
}
</style>
</head>
<body>
<div class="slideshow">
<div class="slide active">
<img src="image1.jpg" alt="Image 1">
<div class="slide-text">
<h1>Slide 1</h1>
<p>This is the first slide.</p>
</div>
</div>
<div class="slide">
<img src="image2.jpg" alt="Image 2">
<div class="slide-text">
<h1>Slide 2</h1>
<p>This is the second slide.</p>
</div>
</div>
<div class="slide">
<img src="image3.jpg" alt="Image 3">
<div class="slide-text">
<h1>Slide 3</h1>
<p>This is the third slide.</p>
</div>
</div>
</div>
<div class="slide-nav">
<button class="active" onclick="changeSlide(0)">1</button>
<button onclick="changeSlide(1)">2</button>
<button onclick="changeSlide(2)">3</button>
</div>
<script>
var slides = document.getElementsByClassName("slide");
var buttons = document.getElementsByClassName("slide-nav")[0].getElementsByTagName("button");
function changeSlide(index) {
for (var i = 0; i < slides.length; i++) {
slides[i].classList.remove("active");
buttons[i].classList.remove("active");
}
slides[index].classList.add("active");
buttons[index].classList.add("active");
}
setInterval(function() {
var currentSlide = document.getElementsByClassName("slide active")[0];
var currentIndex = Array.prototype.indexOf.call(slides, currentSlide);
var nextIndex = (currentIndex + 1) % slides.length;
changeSlide(nextIndex);
}, 5000);
</script>
</body>
</html>
```
这是一个基本的轮播示例,其中包含了三个幻灯片,每个幻灯片都有一张图片和一些文本。通过点击底部的按钮或自动切换,可以切换幻灯片。
阅读全文