使用了, 简单jquery封装函数技巧
效果
html代码
<section class="bannerOpacity banner main">
<h1 class="size_3">bannerOpacity</h1>
<div class="list flex">
<div class="size_3 flex-center active">1</div>
<div class="size_3 flex-center">2</div>
<div class="size_3 flex-center">3</div>
</div>
<!--按钮-->
<div class="banner-btn">
<div class="banner-prev">
<i class="uil uil-angle-left-b"></i>
</div>
<div class="banner-next">
<i class="uil uil-angle-right-b"></i>
</div>
</div>
<!--小圆点-->
<div class="banner-dot flex">
<span class="active"></span>
<span></span>
<span></span>
</div>
</section>
css代码
.main {
display: flex;
justify-content: center;
width: 100%;
}
.banner {
position: relative;
height: 800px;
overflow: hidden;
}
.banner h1 {
position: absolute;
top: 20px;
color: #fff;
z-index: 9;
}
.banner .list {
width: 100%;
height: 100%;
}
.banner .list .active {
opacity: 1;
}
.bannerPosition .list {
position: absolute;
top: 0;
left: 0;
}
.banner .list div {
background: #7059e6;
flex: 0 0 100%;
color: #Fff;
}
.bannerOpacity .list div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: .5s;
opacity: 0;
}
.banner:hover .banner-btn {
opacity: 1;
}
.banner-btn {
transition: .5s;
opacity: 0;
}
.banner-btn > div {
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 999;
}
.banner-btn > div i {
color: #fff;
font-size: 3rem;
cursor: pointer;
}
.banner-prev {
left: 5%;
}
.banner-next {
right: 5%;
}
.banner-dot {
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 10%;
z-index: 99;
gap: 15px;
border-radius: 10px;
}
.banner-dot .active {
background: #333;
}
.banner-dot span {
position: relative;
display: block;
width: 20px;
height: 20px;
border-radius: 10px;
background-color: #Fff;
cursor: pointer;
}
js代码
let timers = null
let index = 0
let list = $('.bannerOpacity .list div');
// 自动轮播
timers = setInterval(() => {
index++
index= index % list.length
getBannerOpacity()
}, 5000)
// 鼠标移入取消定时器
$('.banner').mouseenter(function () {
clearInterval(timers)
// 鼠标移出开启定时器
}).mouseleave(function () {
timers = setInterval(() => {
index++
index = index2 % list.length
getBannerOpacity()
}, 5000)
})
function getBannerOpacity() {
$('.banner .list div').eq(index).addClass('active').siblings().removeClass('active')
$('.banner .banner-dot span').eq(index).addClass('active').siblings().removeClass('active')
}
// 右按钮
$('.banner .banner-next').click(function () {
index++
index= index2 % list.length
getBannerOpacity()
})
// 左按钮
$('.banner .banner-prev').click(function () {
index--
index = (list.length + index) % list.length
getBannerOpacity()
})
// 小圆点
$('.banner .banner-dot span').click(function () {
index = $(this).index()
getBannerOpacity()
})
主页有更多前端交互