使用JS实现轮播图

HTML:

<div class="outer">
        <ul class="img-list">
            <li class="current">
                <a href="#">
                    <img src="./images/1.png" />
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/2.png" />
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/3.png" />
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/4.png" />
                </a>
            </li>
            <li>
                <a href="#">
                    <img src="./images/5.png" />
                </a>
            </li>
        </ul>

        <!-- 图片两边添加切换按钮 -->
        <div class="prev-next">
            <a id="prev" href="javascript:;">&lt;</a>
            <a id="next" href="javascript:;">&gt;</a>
        </div>

        <!-- 导航点 -->
        <div class="dot">
            <a class="active" href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
        </div>
    </div>

CSS:

<style>
        * {
            margin: 0;
            padding: 0;
        }

        img {
            /* 去除图片默认空隙 */
            vertical-align: middle;
        }

        ul {
            list-style: none;
        }

        .outer {
            position: relative;
            width: 640px;
            height: 390px;
            margin: 100px auto;
        }

        .img-list li {
            position: absolute;
            opacity: 0;
            /* 添加过渡 */
            transition: opacity 1s;
        }

        li.current {
            z-index: 1;
            opacity: 1;
        }


        /* 左右两边按钮 */
        .prev-next a {
            font-size: 60px;
            font-weight: 700;
            color: #fff;
            height: 60px;
            z-index: 2;
            text-decoration: none;
            /* 设置垂直居中,要有高度,没高度则盒子被拉长*/
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;

            opacity: .5;
        }

        .prev-next a:hover {
            opacity: 1;
        }

        #next {
            right: 0;
        }

        /* 导航点 */
        .dot {
            /* 设置水平居中,要有宽度,没宽度则盒子被拉长 */
            position: absolute;
            right: 0;
            left: 0;
            bottom: 5px;
            /* margin: auto; */
            display: flex;
            /* 主轴上子元素居中 */
            justify-content: center;
            z-index: 3;
        }

        .dot a {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            margin: 5px;
            background-color: #fff;
            opacity: .5;
        }

        .dot a:hover,
        .dot .active {
            opacity: 1;
        }
    </style>

JS:代码有点乱,可以优化

<script>
        /*
            自动切换图片
        */
        function changeImg(dir) {
            // 获取当前当前显示图片
            const current = document.querySelector(".img-list .current")
            let next

            // 判断参数
            if (dir === "prev") {
                // 获取上一张图片 通过逻辑或中断执行表达式
                next = current.previousElementSibling || document.querySelector(".img-list li:last-child")
            } else if (dir === "next") {
                // 获取下一张图片 通过逻辑或中断执行表达式
                next = current.nextElementSibling || document.querySelector(".img-list li:first-child")
            } else {
                next = document.querySelectorAll(".img-list li")[dir]
            }
            // 获取要显示的图片的索引
            const index = [...document.querySelectorAll(".img-list li")].indexOf(next)
            // 切换状态
            current.classList.remove("current")
            next.classList.add("current")
            // 切换小点
            const currentActive = document.querySelector(".active")
            currentActive.classList.remove("active")

            dots[index].classList.add("active")
        }

        // 函数的闭包
        const toggleChange = (function () {
            let timer = null
            return () => {
                if (timer === null) {
                    timer = setTimeout(function fn() {
                        changeImg("next")
                        timer = setTimeout(fn, 3000)
                    }, 3000)
                } else {
                    clearTimeout(timer)
                    timer = null
                }
            }
        })();

        toggleChange()


        // 当鼠标移进div停止图片切换,移出继续切换
        const outer = document.getElementsByClassName("outer")[0]

        outer.onmouseenter = () => {
            toggleChange()
        }
        outer.onmouseleave = () => {
            toggleChange()
        }

        /* 
            点击按钮切换图片
        */
        const prev = document.getElementById("prev")
        const next = document.getElementById("next")

        prev.onclick = () => {
            changeImg("prev")
        }
        next.onclick = () => {
            changeImg("next")
        }

        /*
            获取五个小点
        */
        const dots = [...document.querySelectorAll(".dot a")]
        document.addEventListener("click", (event) => {
            const index = dots.indexOf(event.target)

            if (index !== -1) {
                /* 切换图片 */
                changeImg(index)
            }
        })
    </script>

持续学习中~

(ps:轮播图JS好难qwq 好绕, 还得多写写)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值