一个页面中有多个轮播图,需要对轮播图进行封装,使它们互不影响。下面是代码:
HTML和CSS样式如下:
<div class="v6_saas_left v6_tonglu_left">
<div class="saas_imgs">
<img class="img_active" src="/statics/output/images/v6_images/saas/s4_1.png" alt="">
<img src="/statics/output/images/v6_images/saas/s4_2.png" alt="">
</div>
<div class="toggle_img_btn">
<span class="toggle_btn_active"></span>
<span></span>
</div>
</div>
样式可能不太齐
.v6_salary_left, .v6_tonglu_left{
position: relative;
width: 500px;
height: 328px;
.saas_imgs{
position: relative;
img{
position: absolute;
z-index: 1;
&.img_active{
z-index: 3;
}
}
}
.toggle_img_btn{
position: absolute;
left: 198px;
bottom: 0;
width: 104px;
height: 4px;
margin: 20px auto 0;
span{
width: 24px;
height: 4px;
float: left;
background-color: rgba(237, 237, 247, 1);
cursor: pointer;
&:nth-child(2n){
margin: 0 16px;
}
}
span.toggle_btn_active{
background-color: #FF5B5F;
}
}
&.v6_tonglu_left{
height: 304px;
}
}
下面是使用JQuery封装
function swiperPlay(box) { // box是轮播图那个盒子的名
var oImg = box.find("img");
var oSpan = box.children(".toggle_img_btn").find("span");
// 切换样式
function fun() {
index++;
if (index == oImg.length) {
index = 0;
}
oImg.removeClass("img_active")
oSpan.removeClass("toggle_btn_active")
oImg.eq(index).addClass("img_active")
oSpan.eq(index).addClass("toggle_btn_active")
}
var index = 0;//定义图片初始化个数
time = setInterval(function () {
fun()
}, 3000);
oSpan.on("click", function () {
console.log(oSpan.index(this))
var oIndex = oSpan.index(this);
clearInterval(time)
oImg.removeClass("img_active")
oSpan.removeClass("toggle_btn_active")
oImg.eq(oIndex).addClass("img_active")
oSpan.eq(oIndex).addClass("toggle_btn_active")
index = oIndex
time = setInterval(function () {
fun()
}, 3000)
})
}
swiperPlay($(".v6_tonglu_left")) // 调用