使用jquery的siblings函数完成图片轮播
时间: 2023-12-02 13:06:05 浏览: 38
好的,我可以为您提供一个简单的使用 jQuery siblings() 函数实现图片轮播的示例:
HTML 代码:
```
<div class="slider">
<img src="image1.jpg" alt="">
<img src="image2.jpg" alt="">
<img src="image3.jpg" alt="">
<img src="image4.jpg" alt="">
</div>
```
CSS 代码:
```
.slider {
position: relative;
width: 600px;
height: 400px;
overflow: hidden;
}
.slider img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 1;
}
.slider img.active {
opacity: 1;
z-index: 2;
}
```
JavaScript 代码:
```
$(function() {
var slider = $('.slider');
var images = slider.find('img');
var currentImage = 0;
var numImages = images.length;
// Set the first image to active
images.eq(0).addClass('active');
// Start the slideshow
setInterval(function() {
// Get the next image
var nextImage = (currentImage + 1) % numImages;
// Fade out the current image
images.eq(currentImage).removeClass('active');
// Fade in the next image
images.eq(nextImage).addClass('active');
// Update the current image
currentImage = nextImage;
}, 5000);
});
```
在这个示例中,我们首先使用 jQuery 的 find() 函数来获取轮播图中的所有图片,并使用 siblings() 函数来控制图片之间的切换。然后,我们设置第一张图片为活动状态,并使用 setInterval() 函数来定期更改当前图片的状态,以实现轮播效果。
相关推荐
![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)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>轮播效果</title> <style> *{ margin: 10; padding: 0; } #banner{ width: 640px; height: 300px; margin: 0 auto; position:relative; } ul{ list-style: none; } .imgList li{ position:absolute; left: 0; top:0; } #left_btn,#right_btn{ width: 38px; height: 61px; display: block; position:absolute; z-index: 2; } #left_btn{ background: url(images/themes.gif); top:125px; } #right_btn{ background: url(images/themes.gif) -38px; right: 0; top:125px; } .focusList{ position: absolute; bottom: 20px; right: 150px; z-index: 3; } .focusList a{ width: 30px; height: 30px; float: left; background: rgba(255, 254, 254, 0.5); border-radius: 50%; margin-left: 10px; } .focusList a.cur{ background: red; } </style> <script src="jquery.js"></script> </head> <body>
<script> console.log($("li")); console.log($(".focusList a")); function changeImg(xiabiao){ $("li").eq(xiabiao).fadeIn().siblings().fadeOut();//siblings()方法返回被选元素的所有同胞元素。 $(".focusList a").eq(xiabiao).addClass("cur").siblings().removeClass("cur"); } var step=0; var timer=null; changeImg(step); function autoPlay(){ timer=setInterval(function() { step++;//每隔一秒要加一 if(step==4){ step=0; } changeImg(step);//调用函数 },1000) } autoPlay(); $("#left_btn").click(function(){ clearInterval(timer); step--; if(step==-1){ step=4; } changeImg(step); autoPlay(); }); $("#right_btn").click(function(){ clearInterval(timer); step++; if(step==5){ step=0; } changeImg(step); autoPlay(); }); $(".focusList a").click(function(){ clearInterval(timer); step=$(this).index();//单击的索引值step传到changeImg(xiabiao) changeImg(step); autoPlay(); }); </script> </body> </html>为什么左右按钮不能实现功能
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![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)
![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)