js原生轮播图自动 通过z-index

这个博客展示了如何使用纯JavaScript来创建一个轮播图。通过改变元素的z-index和opacity属性,实现了图片的切换效果。同时,还包含了前后翻页按钮以及小圆点指示器的功能,点击小圆点或按钮可以手动切换图片,并且有一个自动轮播的定时器。代码中详细注释了各个部分的作用,对于初学者理解JavaScript动态效果的实现非常有帮助。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        .wrap {
            width: 800px;
            height: 400px;
            margin: 0 auto;
            position: relative;
        }

        .uls {
            width: 800px;
            height: 400px;
            list-style: none;
            position: relative;
            padding-left: 0px;
        }

        .lis {
            width: 100%;
            height: 100%;
            position: absolute;
            color: white;
            font-size: 50px;
            opacity: 0;
            transition: all .8s;
        }

        .lis:nth-child(1) {
            background-image: url(./image/7.jpg);
            background-repeat: no-repeat;
            width: 800px;
            height: 400px;

        }

        .lis:nth-child(2) {
            background-image: url(./image/8.jpg);
            background-repeat: no-repeat;
            width: 800px;
            height: 400px;
        }

        .lis:nth-child(3) {
            background-image: url(./image/9.jpg);
            background-repeat: no-repeat;
            width: 800px;
            height: 400px;
        }

        .lis:nth-child(4) {
            background-image: url(./image/10.jpg);
            background-repeat: no-repeat;
            width: 800px;
            height: 400px;
        }

        .lis:nth-child(5) {
            background-image: url(./image/11.jpg);
            background-repeat: no-repeat;
            width: 800px;
            height: 400px;
        }

        .btn {
            width: 50px;
            height: 100px;
            position: absolute;
            top: 150px;
            z-index: 100;
        }

        #pre {
            left: 0px;
        }

        #next {
            right: 0px;
        }

        .lis.active {
            opacity: 1;
            z-index: 10;
        }

        .dian {
            list-style: none;
            padding-left: 0;
            position: absolute;
            right: 20px;
            bottom: 10px;
            z-index: 1000;
        }

        .dians {
            width: 10px;
            height: 10px;
            border-radius: 100%;
            float: left;
            margin-left: 14px;
            background-color: slategrey;
            border: solid white 2px;
        }

        .dians.active {
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="uls">
            <li class="lis active">0</li>
            <li class="lis">1</li>
            <li class="lis">2</li>
            <li class="lis">3</li>
            <li class="lis">4</li>
        </ul>
        <ul class="dian">
            <li class="dians active" data-index="0"></li>
            <li class="dians" data-index="1"></li>
            <li class="dians" data-index="2"></li>
            <li class="dians" data-index="3"></li>
            <li class="dians" data-index="4"></li>
        </ul>
        <button type="button" class="btn" id="pre">《</button>
        <button type="button" class="btn" id="next">》</button>
    </div>
</body>
<script>
    var lis = document.getElementsByClassName('lis');//图片
    var dians = document.getElementsByClassName('dians');//点
    var goprebtn = document.getElementById('pre');//上一页按钮
    var gonextbtn = document.getElementById('next');//下一页按钮

    var index = 0
//清除图片class里面的名字
    var clearactive = function () {
        for (var i = 0; i < lis.length; i++) {
            lis[i].className = 'lis';
        }
        for (var i = 0; i < dians.length; i++) {
            dians[i].className = 'dians';
        }
    }
//给class 添加 active
    var goindex = function () {
        clearactive()
        dians[index].className = 'dians active';
        lis[index].className = 'lis active';
    }
//判断页数 
    var gonext = function () {
        if (index < 4) {
            index++;
        } else {
            index = 0;
        }
        goindex();
    }

    var gopre = function () {
        if (index == 0) {
            index = 4;
        } else {
            index--;
        }
        goindex();
    }
//监听事件
    gonextbtn.addEventListener('click', function () {
        gonext();
        time = 0;
    })

    goprebtn.addEventListener('click', function () {
        gopre();
        time = 0;
    })

    for (var i = 0; i < dians.length; i++) {
        dians[i].addEventListener('click', function () {
            var dianss = this.getAttribute('data-index');
            index = dianss;
            goindex();
            time = 0;
        })

    }
    //定时器
    var time = 0;
    setInterval(function () {
        time++;
        if (time == 20) {
            gonext();
            time = 0;
        }
    }, 100)
</script>


</html>

js原生轮播图通过隐藏现实z-index实现

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值