js简单基础轮播图(手动)

<div id="photo">
        <p id="p"></p>
        <img src="./image/01.jpg" alt="">//图片存储路径
        <div class="btn">
            <button id="prev">上一张</button>
            <button id="next">下一张</button>
        </div>
    </div>

css部分:

#p {
            margin: 0;
            color: white;
            padding: 5px;
        }

        #photo {
            width: 360px;
            height: 360px;
            margin: 50px auto;
            text-align: center;
            background-color: skyblue;
            padding: 10px;
        }

        img {
            width: 300px;
            height: 300px;
        }

        .btn {
            width: 350px;
            height: 25px;
            justify-content: space-around;
            display: flex;
        }

js代码:

window.onload = function () {
            var arr = ["./image/01.jpg", "./image/02.jpg", "./image/03.jpg", "./image/04.jpg", "./image/05.jpg", "./image/06.jpg"];
            var index = 0;
            var text = document.getElementById("p");
            text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            var prev = document.getElementById("prev");
            var next = document.getElementById("next");
            var img = document.getElementsByTagName("img")[0];
            prev.onclick = function () {
                if (index == 0) {
                    index = arr.length;
                }
                index--
                img.src = arr[index]
                text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            }
            next.onclick = function () {
                if (index == arr.length - 1) {
                    index = -1;
                }
                index++
                img.src = arr[index]
                text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            }
}

呈现效果:

全部代码:

<!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>
        #p {
            margin: 0;
            color: white;
            padding: 5px;
        }

        #photo {
            width: 360px;
            height: 360px;
            margin: 50px auto;
            text-align: center;
            background-color: skyblue;
            padding: 10px;
        }

        img {
            width: 300px;
            height: 300px;
        }

        .btn {
            width: 350px;
            height: 25px;
            justify-content: space-around;
            display: flex;
        }
    </style>
</head>

<body>
    <script>
        window.onload = function () {
            var arr = ["./image/01.jpg", "./image/02.jpg", "./image/03.jpg", "./image/04.jpg", "./image/05.jpg", "./image/06.jpg"];
            var index = 0;
            var text = document.getElementById("p");
            text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            var prev = document.getElementById("prev");
            var next = document.getElementById("next");
            var img = document.getElementsByTagName("img")[0];
            prev.onclick = function () {
                if (index == 0) {
                    index = arr.length;
                }
                index--
                img.src = arr[index]
                text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            }
            next.onclick = function () {
                if (index == arr.length - 1) {
                    index = -1;
                }
                index++
                img.src = arr[index]
                text.innerHTML = "一共有" + arr.length + "张图片," + "现在是第" + (index + 1) + "张"
            }
}
    </script>
    <div id="photo">
        <p id="p"></p>
        <img src="./image/01.jpg" alt="">
        <div class="btn">
            <button id="prev">上一张</button>
            <button id="next">下一张</button>
        </div>
    </div>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值