html图片自动轮播代码
时间: 2023-11-14 22:08:26 浏览: 90
以下是一个基本的 HTML 图片自动轮播代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>图片自动轮播</title>
<style>
.slideshow-container {
max-width: 500px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.slideshow-container img {
width: 100%;
height: 100%;
}
.dot-container {
text-align: center;
margin: 20px 0;
}
.dot {
height: 15px;
width: 15px;
margin: 0 5px;
border-radius: 50%;
display: inline-block;
background-color: #bbb;
transition: background-color 0.5s ease;
cursor: pointer;
}
.active,
.dot:hover {
background-color: #717171;
}
</style>
</head>
<body>
<div class="slideshow-container">
<img class="mySlides" src="img1.jpg">
<img class="mySlides" src="img2.jpg">
<img class="mySlides" src="img3.jpg">
<div class="dot-container">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
</div>
<script>
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 3000); // 切换图片的时间间隔,单位为毫秒
}
</script>
</body>
</html>
```
这个代码使用了 JavaScript 来实现自动轮播,每隔 3 秒钟就自动切换图片。你可以根据自己的需求来修改切换时间间隔等参数。
阅读全文