10屏轮播图自动播放一(JavaScript)

60 阅读1分钟

1.jpg

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>10屏轮播图</title>
    <style>
        .img-box {
            width: 700px;
            height: 320px;
            margin: 50px auto 0;
            background: #000;
            position: relative;
        }
        
        .img-box .tip {
            width: 700px;
            height: 53px;
            line-height: 53px;
            position: absolute;
            bottom: 0px;
            background-color: rgba(0, 0, 0, 0.8);
            z-index: 10;
        }
        
        .img-box .tip h3 {
            width: 82%;
            margin: 0;
            margin-right: 20px;
            padding-left: 20px;
            color: #98E404;
            font-size: 28px;
            float: left;
            font-weight: 500;
            font-family: "Microsoft Yahei", Tahoma, Geneva;
        }
        
        .img-box .tip a {
            width: 30px;
            height: 29px;
            display: block;
            float: left;
            margin-top: 12px;
            margin-right: 3px;
        }
        
        .img-box ul {
            position: absolute;
            bottom: 0;
            right: 30px;
            list-style: none;
            z-index: 99;
        }
    </style>
</head>

<body>
    <div class="img-box">
        <img class="pic" src="images/b01.jpg" alt="第1张图的描述信息">
        <div class="tip">
            <h3 class="text">挑战云歌单,欢迎你来</h3>
        </div>
    </div>

    <script>
        // 数据
        const data = [{
            imgSrc: 'images/b01.jpg',
            title: '挑战云歌单,欢迎你来'
        }, {
            imgSrc: 'images/b02.jpg',
            title: '田园日记,上演上京记'
        }, {
            imgSrc: 'images/b03.jpg',
            title: '甜蜜攻势再次回归'
        }, {
            imgSrc: 'images/b04.jpg',
            title: '我为歌狂,生为歌王'
        }, {
            imgSrc: 'images/b05.jpg',
            title: '年度校园主题活动'
        }, {
            imgSrc: 'images/b06.jpg',
            title: 'pink老师新歌发布,5月10号正式推出'
        }, {
            imgSrc: 'images/b07.jpg',
            title: '动力火车来到西安'
        }, {
            imgSrc: 'images/b08.jpg',
            title: '钢铁侠3,英雄镇东风'
        }, {
            imgSrc: 'images/b09.jpg',
            title: '我用整颗心来等你'
        }, ]

        //获取 事件源
        const pic = document.querySelector('.pic')
        const h3 = document.querySelector('.text')
            //查看是否拿到事件              
        console.log(pic)
        console.log(h3)
            //开始图片轮播部分
        let i = 0
            //开启定时器
        setInterval(function() {
        //加入轮播相关条件
            i++
            if (i >= data.length) {
                i = 0
            }
            pic.src = data[i].imgSrc
            h3.innerHTML = data[i].title
        }, 1000)
    </script>
</body>

总结 1.获取DOM 修改元素 2.中间若穿插计时器

setInterval(function() {      
    
    }, 1000)

关闭定时器 在结尾处添加 clearInterval(timer) 并且在函数开头定义 let timer= 即可

可以把修改元素这一步放在这步中 3.同常元素都是通过数组的形式确定更换的,以数组索引号来进行查询并更换(或者以条件等形式)**