有如下这些规格一致的图片:mary.jpg tom.jpg lucy.jpg jack.jpg 请使用以上图片来制作一个图片轮播效果代码
时间: 2023-04-03 13:05:25 浏览: 112
实现图片轮播的源代码!
4星 · 用户满意度95%
以下是一个简单的图片轮播效果代码,使用了以上提到的四张图片:
HTML 代码:
```
<div class="slideshow-container">
<div class="mySlides fade">
<img src="mary.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="tom.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="lucy.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="jack.jpg" style="width:100%">
</div>
</div>
<br>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
```
CSS 代码:
```
.slideshow-container {
max-width: 500px;
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;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
.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 {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
```
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";
}
```
这个代码使用了 JavaScript 来控制图片轮播的效果,CSS 用来设置样式,HTML 用来定义轮播的图片和按钮。
阅读全文