写一个轮播图的代码
时间: 2023-12-01 08:12:27 浏览: 21
以下是一个简单的轮播图代码,使用了HTML、CSS和JavaScript:
HTML代码:
```html
<div class="slideshow-container">
<div class="mySlides fade">
<img src="image1.jpg">
</div>
<div class="mySlides fade">
<img src="image2.jpg">
</div>
<div class="mySlides fade">
<img src="image3.jpg">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
```
CSS代码:
```css
.slideshow-container {
position: relative;
width: 100%;
height: 500px;
}
.mySlides {
display: none;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.prev, .next {
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;
cursor: pointer;
z-index: 1;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1s;
animation-name: fade;
animation-duration: 1s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
```
JavaScript代码:
```javascript
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex-1].style.display = "block";
}
```
该代码使用了CSS动画,将轮播图的图片淡入淡出,同时添加了左右箭头,可以通过点击箭头来切换轮播图片。