HTML + CSS + js 实现轮播图的代码
时间: 2023-09-24 22:06:58 浏览: 122
好的,以下是HTML、CSS、JavaScript实现轮播图的代码:
HTML代码:
```
<div class="slideshow-container">
<div class="mySlides fade">
<img src="image1.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="image2.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="image3.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
```
CSS代码:
```
* {box-sizing:border-box}
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display:none;
}
.prev, .next {
cursor:pointer;
position:absolute;
top:50%;
width:auto;
margin-top:-22px;
padding:16px;
color:white;
font-weight:bold;
font-size:18px;
transition:0.6s ease;
border-radius:0 3px 3px 0;
user-select:none;
}
.next {
right:0;
border-radius:3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color:rgba(0,0,0,0.8);
}
.dot-container {
text-align:center;
padding:20px;
background: #ddd;
}
.dot {
cursor:pointer;
height:15px;
width:15px;
margin:0 2px;
background-color:#bbb;
border-radius:50%;
display:inline-block;
transition:background-color 0.6s ease;
}
.active, .dot:hover {
background-color:#717171;
}
```
JavaScript代码:
```
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";
}
```
希望能帮到您!
阅读全文