帮我用js写一个轮播图 要求会动态滚动
时间: 2024-06-10 07:09:04 浏览: 122
当然,我可以为您提供代码示例。以下是一个简单的基于JavaScript的轮播图实现:
HTML:
```
<div class="slideshow-container">
<div class="mySlides">
<img src="img1.jpg">
</div>
<div class="mySlides">
<img src="img2.jpg">
</div>
<div class="mySlides">
<img src="img3.jpg">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
```
CSS:
```
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
text-align: center;
}
img {
max-width: 100%;
height: auto;
vertical-align: middle;
}
.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);
}
```
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";
}
```
这是一个基本的轮播图实现,您可以根据需要对其进行修改和定制。
阅读全文