用jQuery写一个轮播图代码
时间: 2023-03-31 19:03:43 浏览: 102
以下是一个简单的 jQuery 轮播图代码:
HTML 代码:
```
<div class="slider">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
```
CSS 代码:
```
.slider {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
}
.slider img {
position: absolute;
top: ;
left: ;
width: 100%;
height: 100%;
opacity: ;
transition: opacity 1s ease-in-out;
}
.slider img.active {
opacity: 1;
}
```
jQuery 代码:
```
$(document).ready(function() {
var slideIndex = ;
showSlides();
function showSlides() {
var slides = $(".slider img");
for (var i = ; i < slides.length; i++) {
slides[i].classList.remove("active");
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
slides[slideIndex - 1].classList.add("active");
setTimeout(showSlides, 500);
}
});
```
阅读全文