JS+CSS+HTML写出一个完整的轮播图
时间: 2023-06-28 10:15:40 浏览: 52
好的,请参考以下代码实现一个完整的轮播图:
HTML:
```html
<div class="slideshow-container">
<img src="image1.jpg" class="slides">
<img src="image2.jpg" class="slides">
<img src="image3.jpg" class="slides">
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
```
CSS:
```css
.slideshow-container {
max-width: 800px;
position: relative;
margin: auto;
}
.slides {
width: 100%;
height: auto;
display: none;
position: absolute;
top: 0;
left: 0;
}
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
.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;
margin-top: 20px;
}
.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;
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
```
JavaScript:
```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("slides");
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";
}
```
这样就完成了一个基本的轮播图,可以通过 CSS 文件中的样式调整轮播图的样式,通过 JavaScript 文件中的代码修改轮播图的功能。