做一个jQuery图片切换效果
时间: 2023-05-21 14:03:47 浏览: 119
好的,以下是一个简单的 jQuery 图片切换效果的代码:
HTML 代码:
```
<div class="slideshow">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
```
CSS 代码:
```
.slideshow {
position: relative;
height: 400px;
}
.slideshow img {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slideshow img.active {
opacity: 1;
}
```
jQuery 代码:
```
$(document).ready(function() {
$('.slideshow img:first-child').addClass('active');
setInterval(function() {
var current = $('.slideshow img.active');
var next = current.next();
if (next.length === 0) {
next = $('.slideshow img:first-child');
}
current.removeClass('active');
next.addClass('active');
}, 3000);
});
```
这个代码会在页面加载完成后,将第一张图片设为 active,然后每隔 3 秒钟切换到下一张图片。
阅读全文