【js案例】定时版轮播图

案例实现每过两秒对图中图片,文案,文字背景色,底部小圆点进行改变:

效果图:

全代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .wrap{
        box-sizing: border-box;
    }
    .slider{
        margin: auto;
        width: 560px;
        height: 400px;
        overflow: hidden;
    }
    .slider-wrapper{
        width: 100%;
        height: 320px;
    }
    .slider-wrapper img{
        width: 100%;
        height: 100%;
        display: block;
    }
    .footer{
        height: 80px;
        background-color:rgb(36,31,33);
        padding: 12px 12px 0 12px;
        position: relative;
    }
    p{
        margin: 0;
        color: white;
        font-size: 18px;
        margin-bottom: 10px;
    }
    .indicator{
        margin: 0;
        padding: 0;
        list-style: none;
        display: flex;
        align-items: center;
    }
    .indicator li{
        width: 8px;
        height: 8px;
        margin: 4px;
        border-radius: 50%;
        background: #fff;
        opacity: 0.4;
        cursor: pointer;
    }
    ul .active{
        width: 10px;
        height: 10px;
        opacity: 1;
    }
</style>
<body>
    <div class="wrap">

        <div class="slider">
            <div class="slider-wrapper">
                <img src="./img/1.jpg" alt="">
            </div>
            <div class="footer">
                <p>天使</p>
                <ul class="indicator">
                    <li class="active"></li>
                    <li></li>
                    <li></li>
                    <li></li>
                    <li></li>
                </ul>
            </div>
        </div>


    </div>
</body>
<script>
//创建对象数组,存储切换后的图片,文案,背景色
    const sliderData = [
        {url:'./img/1.jpg',title:'天使',color:'rgb(36,31,33)'},
        {url:'./img/2.jpg',title:'幽灵',color:'rgb(166,131,143)'},
        {url:'./img/3.jpg',title:'boom',color:'rgb(36,31,33)'},
        {url:'./img/4.jpg',title:'深海',color:'rgb(43,35,26)'},
        {url:'./img/5.jpg',title:'anger',color:'rgb(99,72,114)'}
    ]
//获取css标签
    const img = document.querySelector('img')
    const title = document.querySelector('.footer p')
    const color = document.querySelector('.footer')
//使用间歇函数
    let i = 0
    setInterval(function(){
        i++;
        img.src = sliderData[i].url
        title.innerHTML = sliderData[i].title
        color.style.backgroundColor = sliderData[i].color
//对底部小圆点进行样式切换
        document.querySelector('ul .active').classList.remove('active')
        document.querySelector(`ul li:nth-child(${i+1})`).classList.add('active')

        if(i==4){
            i=-1;
        }
    },2000)
</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值