左边文字右边图片用定时器轮播jQuery写
时间: 2023-07-18 16:09:05 浏览: 52
好的,下面是一个简单的左边文字右边图片用定时器轮播的jQuery代码:
HTML代码:
```
<div class="slider-wrapper">
<div class="slider">
<div class="slide">
<img src="image1.jpg" alt="Image 1">
<div class="caption">Text 1</div>
</div>
<div class="slide">
<img src="image2.jpg" alt="Image 2">
<div class="caption">Text 2</div>
</div>
<div class="slide">
<img src="image3.jpg" alt="Image 3">
<div class="caption">Text 3</div>
</div>
</div>
</div>
```
CSS代码:
```
.slider-wrapper {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
}
.slider {
position: absolute;
top: 0;
left: 0;
width: 300%;
height: 100%;
display: flex;
}
.slide {
position: relative;
width: 33.333%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.slide img {
max-width: 100%;
max-height: 100%;
display: block;
}
.caption {
position: absolute;
bottom: 10%;
left: 50%;
transform: translateX(-50%);
font-size: 2em;
color: #fff;
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
text-align: center;
z-index: 1;
}
```
jQuery代码:
```
$(document).ready(function() {
var slideCount = $('.slider').children().length;
var slideWidth = $('.slider-wrapper').width() / 3;
var slideIndex = 0;
$('.slide').css('width', slideWidth);
function slide() {
$('.slider').animate({
left: -slideIndex * slideWidth
}, 500);
$('.caption').fadeOut(500).eq(slideIndex).fadeIn(500);
}
function nextSlide() {
slideIndex++;
if (slideIndex >= slideCount) {
slideIndex = 0;
}
slide();
}
var timerId = setInterval(nextSlide, 3000);
});
```
注释:
1. `slideCount` 表示轮播图的数量,这里为 3。
2. `slideWidth` 表示每个轮播图的宽度,这里为轮播容器 `.slider-wrapper` 的宽度除以 3。
3. `slideIndex` 表示当前轮播图的索引,初始值为 0。
4. `$('.slide').css('width', slideWidth);` 设置每个轮播图的宽度为 `slideWidth`。
5. `slide()` 是核心函数,用来切换轮播图和对应文字。使用 `animate()` 方法将 `.slider` 容器向左移动 `slideIndex * slideWidth`,使用 `fadeOut()` 和 `fadeIn()` 方法切换文字。
6. `nextSlide()` 函数用来切换到下一个轮播图。如果已经到达最后一个轮播图,将 `slideIndex` 重置为 0。
7. `setInterval(nextSlide, 3000)` 用来定时切换轮播图,间隔为 3 秒。将返回的定时器 ID 存储在变量 `timerId` 中,方便后续操作。
希望这个示例能够帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)