用css写一个网页轮播图
时间: 2023-09-24 21:02:35 浏览: 79
好的,以下是一个简单的 CSS 实现网页轮播图的示例代码:
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>
```
CSS 部分:
```
.slideshow-container {
position: relative;
margin: auto;
}
.mySlides {
display: none;
text-align: center;
}
.mySlides img {
vertical-align: middle;
}
.prev, .next {
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;
cursor: pointer;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: .4;
}
to {
opacity: 1;
}
}
```
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";
}
```
注意:这只是一个简单的示例,实际应用中可能需要根据具体需求进行修改。
阅读全文