面向对象简易轮播图实现

本文介绍了一种面向对象的方法来实现简易轮播图。通过遍历图片获取索引,创建并绑定下方按钮,调整父元素的left间距以实现图片切换。同时,文章还探讨了自动轮播、获取图片索引、防止循环显示超出范围的难点,以及在鼠标悬停时暂停和离开时恢复计时器的小细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

        根据图片遍历获得索引,

        利用索引创建绑定下方按钮,

        通过移动图片父元素的左间距实现图片切换,

        自动调用右切图方法实现自动轮播。

css部分

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

        li {
            list-style: none;
        }

        #box {
            width: 300px;
            height: 300px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto auto;
            border: 2px solid skyblue;
            padding: 20px;
            /* position: relative; */
        }

        .button {
            height: 60px;
            width: 300px;
            position: absolute;
            left: 0;
            top: 135px;
            display: flex;
            justify-content: space-between;
            margin: 0 20px;
            /* background: black; */
            display: none;
            /* display: block; */
        }

        .button span {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            opacity: 0.8;
            background: white;
            font-size: 60px;
            color: black;
            line-height: 50px;
            text-align: center;

        }

        #left {
            position: absolute;
            left: 0;
        }

        #right {
            position: absolute;
            right: 0;
        }

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

        ul {
            display: flex;
            position: relative;
            left: 0;
        }

        ol {
            display: flex;
            position: absolute;
            right: 22px;
            bottom: 30px;
            /* background: black; */

        }

        ol li {
            width: 30px;
            height: 30px;
            background: white;
            border: 1px solid black;
            margin-left: 10px;
            line-height: 30px;
            text-align: center;
        }

        .color {
            background: yellow;
        }

        .screen {
            display: block;
            overflow: hidden;
        }
    </style>

html部分

图片部分需要引用自己的图片,修改地址即可

<div id="box">
        <div class="screen">
            <ul>
                <li><img src="../../images/77.jpg" alt=""></li>
                <li><img src="../../images/hutao1.jpg" alt=""></li>
                <li><img src="../../images/qin.jpg" alt=""></li>
                <li><img src="../../images/ganyu.jpg" alt=""></li>
                <li><img src="../../images/mona.jpg" alt=""></li>
            </ul>
            <ol>

            </ol>
        </div>
        <div class="button">
            <span id="left">
                <</span> <span id="right">>
            </span>
        </div>
    </div>

js部分

    <script>
        let box = document.querySelector("#box")
        box.addEventListener
        class move {
            constructor() {
                this.box = this.$("#box");
                this.ul = this.$("ul")
                this.ol = this.$("ol");
                this.btn = this.$(".button");
                this.li = this.$("ul li");
                this.img = this.$("img");
                this.left = this.$("#left");
                this.right = this.$("#right");
                //用于储存遍历
                this.index = 0;
                //用处储存计时器
                this.timess = 0;

                this.ulLi = this.ul.children;
                this.olLi = this.ol.children;
                this.imgW = this.img.offsetWidth;
                this.box.addEventListener("mouseover", this.overFn.bind(this));
                this.box.addEventListener("mouseout", this.outFn.bind(this));
                this.right.addEventListener("click", this.clickFnR.bind(this));
                this.left.addEventListener("click", this.clickFnL.bind(this));
                //默认调用运动函数,自动轮播
                this.auto();

                //遍历绑定下方按键方法
                for (let i = 0; i < this.ulLi.length; i++) {
                    //创建按键li
                    this.newLi = document.createElement("li");
                    // 给予每个li一个innerHTML值
                    this.newLi.innerHTML = i + 1;
                    //将新增的按键添加到this.ol中
                    this.ol.appendChild(this.newLi);
                    //默认给予第一张对应按键color类
                    i == 0 && this.newLi.classList.add("color");
                    //绑定点击二级事件,并传递i的值,(做到查找当前图片位置)
                    this.newLi.addEventListener("click", this.clickFn.bind(this, i));
                }
            }
            //左右按钮的出现方法
            overFn() {
                this.btn.style.display = "block";
                clearInterval(this.timess);
            }
            //左右按钮的消失方法
            outFn() {
                this.btn.style.display = "none";
                this.auto();
            }
            //带数字按键的方法
            clickFn(i) {
                //获取左移距离、设置左移距离
                //拿到遍历,给予this.index这个遍历值
                this.index = i;
                this.classOn(i);
            }

            //点击右箭头方法
            clickFnR() {
                this.index++;
                //判断是否达到最右边图片,如达到就跳转到第一张.
                if (this.index == this.ulLi.length) this.index = 0;
                let i = this.index;
                this.classOn(i);
            }

            //点击左箭头方法
            clickFnL() {
                this.index--;
                //判断是否达到最左边图片,如达到就跳转到最后一张
                if (this.index == -1) this.index = this.ulLi.length - 1;
                let i = this.index;
                this.classOn(i);
            }
            //按钮被选中方法
            classOn(i) {
                //图片移动
                let tmpx = this.imgW * i;
                this.ul.style.left = -tmpx + "px";
                
                //给予当前color类清除color类,
                this.$(".color").classList.remove("color");
                //给予当前点击按键color类
                this.olLi[i].classList.add("color");

            }
            // 自动轮播
            auto() {
                this.timess = setInterval(() => {
                    //定时调用右箭头方法
                    this.clickFnR();
                }, 3000)
            }

            //节点绑定方法
            $(tag) {
                return document.querySelector(tag);
            }
        }
        new move;
    </script>

难点:

        如何获取图片索引,并在其他方法中调用

        判断自加索引是否达到尽头,避免不能循环显示

        小细节:移入需要暂定计时器,移出恢复计时器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值