dom版本的轮播图

本文详细介绍了如何利用纯CSS3和HTML来创建一个动态的DOM版本轮播图,通过变换、过渡和定位等技术实现图片的平滑切换效果,无需额外的JavaScript库。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 600px;
            border: 1px solid;
            margin: auto;
            position: relative;
            font-size: 0;
        }

        li {
            list-style: none;
        }

        ul,
        ol {
            width: 100%;
            box-sizing: border-box;
            display: flex;
            position: absolute;
        }

        ul {

            justify-content: space-between;
            padding: 0 20px;
            top: calc(50% - 25px);
        }

        ul>li {
            width: 49px;
            height: 49px;
            background: url("../img/shutter_prevBtn.png");
        }

        ul>li:hover {
            background-position: 0 -49px;
        }

        ul>li:nth-child(2) {
            transform: rotate(180deg);
        }

        ol {
            top: 90%;
            justify-content: center;

        }

        ol>li {
            width: 15px;
            height: 15px;
            background-color: rgba(255, 255, 255, .4);
            border-radius: 50%;
            margin-left: 10px;
        }

        ol>li:hover {
            background-color: #fff;
        }

        ol>li:first-child {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="container">
        <img src="../img/下载 (1).jfif" alt="">
        <ul>
            <li></li>
            <li></li>
        </ul>
        <ol>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
    </div>
    <script>
        const img = document.querySelector(".container>img");
        const container = document.querySelector(".container"); // div
        const next_btn = document.querySelectorAll("ul>li")[1]; // 右键
        const pre_btn = document.querySelectorAll("ul>li")[0]; // 左键
        const lis = document.querySelectorAll("ol>li"); // 所有 li

        const arr = ['下载 (1).jfif', '下载 (2).jfif', '下载 (3).jfif', '下载.jfif'];

        let i = 0, timer = null;

        function render() {
            i++;
            if (i > arr.length - 1) {
                i = 0;
            }
            img.src = `../img/${arr[i]}`;
            for (let item of lis) {
                item.style.backgroundColor = '#fff';
            }
            lis[i].style.backgroundColor = 'red';
        }
        function reverse() {
            i--;
            if (i < 0) {
                i = arr.length - 1;
            }
            img.src = `../img/${arr[i]}`;
        }

        timer = setInterval(render, 1000);

        // 移入停止
        container.onmouseenter = function () {
            clearInterval(timer);
        }
        container.onmouseleave = function () {
            timer = setInterval(render, 1000);
        }
        // 右翻
        next_btn.onclick = function () {
            render();
        }
        // 左翻
        pre_btn.onclick = function () {
            reverse();
        }
        // 圆点点击
        for (let index in lis) {
            lis[index].onclick = function () {
                console.log(index);
                i = index-1;
                render();
            }
        }



    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值