生成 html轮播图
时间: 2024-01-09 13:22:09 浏览: 117
以下是生成HTML轮播图的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>HTML轮播图</title>
<style>
.slideshow {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slideshow img {
width: 100%;
height: 100%;
}
.dots {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.dot {
width: 10px;
height: 10px;
border-radius: 50%;
background-color: gray;
margin: 0 5px;
cursor: pointer;
}
.dot.active {
background-color: black;
}
</style>
</head>
<body>
<div class="slideshow">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
<div class="dots">
<div class="dot active"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
<script>
var slides = document.querySelectorAll('.slideshow img');
var dots = document.querySelectorAll('.dot');
var currentSlide = 0;
function showSlide(n) {
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = 'none';
}
for (var i = 0; i < dots.length; i++) {
dots[i].classList.remove('active');
}
slides[n].style.display = 'block';
dots[n].classList.add('active');
}
function nextSlide() {
currentSlide++;
if (currentSlide >= slides.length) {
currentSlide = 0;
}
showSlide(currentSlide);
}
function previousSlide() {
currentSlide--;
if (currentSlide < 0) {
currentSlide = slides.length - 1;
}
showSlide(currentSlide);
}
function selectSlide(n) {
currentSlide = n;
showSlide(currentSlide);
}
setInterval(nextSlide, 3000);
</script>
</body>
</html>
```
这段代码会生成一个带有轮播图和动态生成小圆点的HTML页面。轮播图中的图片可以根据实际情况进行替换,小圆点的数量会根据图片数量自动生成,并且可以点击小圆点切换图片。
阅读全文