浅入浅出轮播图(简单易学)

使用了, 简单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()
})

主页有更多前端交互

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋风.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值