自动轮播图

<!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>Document</title>

    <style>

        * {

            margin: 0;

            padding: 0;

            list-style: none;

        }

        div {

            position: relative;

            width: 500px;

            height: 300px;

            margin: 0 auto;

        }

        img {

            width: 500px;

            height: 300px;

        }

        button {

            width: 20px;

            height: 50px;

            background-color: rgba(0, 0, 0, 0.5);

            color: pink;

            border: 0;

        }

        .left {

            position: absolute;

            top: 50%;

            left: 0;

            margin-top: -25px;

        }

        .right {

            position: absolute;

            top: 50%;

            right: 0;

            margin-top: -25px;

        }

        h1 {

            position: absolute;

            top: 0;

            left: 50%;

            font-size: 16px;

            margin-left: -8px;

            color: red;

        }

        em {

            font-style: normal;

            position: absolute;

            bottom: 0;

            left: 50%;

            margin-left: -8px;

            color: red;

        }

        ul {

            width: 100px;

            display: flex;

            justify-content: space-between;

            position: absolute;

            left: 50%;

            margin-left: -50px;

            bottom: 0px;

        }

        li {

            width: 20px;

            height: 20px;

            border-radius: 50%;

            background-color: #ccc;

            margin-bottom: 15px;

        }

        .active {

            background-color: pink;

        }

    </style>

    </style>

</head>

<body>

    <div>

        <img src="../作业/img/1.webp" alt="">

        <button class="left">< </button>

        <button class="right"> > </button>

        <h1>1/4</h1>

        <em>动漫1</em>

        <ul>

        <li class="active"></li>

        <li></li>

        <li></li>

        <li></li>

        </ul>

    </div>

    <script>

        var img = document.getElementsByTagName("img")[0]

        var btn = document.getElementsByTagName("button")

        var h1 = document.getElementsByTagName("h1")[0]

        var em = document.getElementsByTagName("em")[0]

        var li = document.getElementsByTagName("li")

        var arr = ["../作业/img/1.webp", "../作业/img/2.webp", "../作业/img/3.webp", "../作业/img/4.webp"]

        var n = 1

        btn[1].onclick = function () {

            timer()

        }

        btn[0].onclick = function () {

            n--

            if (n < 1) {

                n = 4

            }

            img.src = arr[n - 1]

            h1.innerHTML = n + "/4"

            em.innerHTML = "动漫" + n

            for (var i = 0; i < li.length; i++) {

                li[i].className = ""

            }

            li[n - 1].className = "active"

        }

        for (var i = 0; i < li.length; i++) {

            li[i].index=i

            li[i].onclick = function () {

                for (var j = 0; j < li.length; j++) {

                    li[j].className = ""

                }

                this.className = "active"

                var a = this.index

                img.src=arr[a]

                h1.innerHTML=a+1+"/4"

                em.innerHTML="动漫"+(a+1)

            }

        }

        function timer(){

            n++

            if (n > 4) {

                n = 1

            }

            img.src = arr[n - 1]

            h1.innerHTML = n + "/4"

            em.innerHTML = "动漫" + n

            for (var i = 0; i < li.length; i++) {

                li[i].className = ""

            }

            li[n - 1].className = "active"

        }

        var time = setInterval(timer,3000)

        img.onmouseover=function(){

            clearInterval(time)

        }

        img.onmouseout=function(){

            time = setInterval(timer,3000)

        }

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值