面向对象 -- 轮播图

<!DOCTYPE html>
<html lang="en">

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

        .lunbo {
            width: 650px;
            height: 360px;
            border: 1px solid #000;
            margin: 0 auto;
            position: relative;
            margin-top: 50px;
        }

        .lunbo ul li {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
        }

        .lunbo img {
            width: 650px;
            height: 360px;
            display: block;
        }

        .lunbo ol {
            position: absolute;
            left: 280px;
            bottom: 20px;
        }

        .lunbo ol li {
            width: 10px;
            height: 10px;
            border: 2px solid #000;
            border-radius: 50%;
            margin-right: 5px;
            float: left;
            cursor: pointer;
        }

        .lunbo ol li.active {
            background-color: orangered;
        }

        .left,
        .right {
            font-size: 60px;
            color: #fff;
            text-decoration: none;
            position: absolute;
            top: 50%;
            margin-top: -30px;

        }

        .left {
            left: 25px;
        }

        .right {
            right: 25px;
        }
    </style>
</head>

<body>
    <div class="lunbo">
        <ul>
            <li style="opacity: 1;"><img src="../images/1.jpg" alt=""></li>
            <li><img src="../images/2.jpg" alt=""></li>
            <li><img src="../images/3.jpg" alt=""></li>
            <li><img src="../images/4.jpg" alt=""></li>
            <li><img src="../images/5.jpg" alt=""></li>
        </ul>

        <ol>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <a href="javascript:;" class="left">&lt;</a>

        <a href="javascript:;" class="right">&gt;</a>
    </div>
</body>
<script>
    function LunBo() {
        this.LunBo = document.querySelector('.lunbo')
        this.oImg = document.querySelectorAll('.lunbo ul li');//图片
        this.oBtn = document.querySelectorAll('.lunbo ol li');//按钮
        this.leftBtn = document.querySelector('.left');//左按钮
        this.rightBtn = document.querySelector('.right');//右按钮
        this.index = 0;
    }

    LunBo.prototype.init = function () {
        // console.log(999);
        let _this = this;
        for (let i = 0; i < _this.oBtn.length; i++) {
            this.oBtn[i].onmouseover = function () {
                _this.index = i;
                _this.fn();
            }
        }
        this.rightBtn.onclick = function () {
            // console.log(666);
            _this.index++;
            if (_this.index > _this.oImg.length - 1) {
                _this.index = 0;
            }
            _this.fn();
        }

        this.leftBtn.onclick = function () {
            _this.index--;
            if (_this.index < 0) {
                _this.index = _this.oImg.length - 1;
            }
            _this.fn();
        }
        //自动轮播
        _this.selfMotion()
        //停止轮播
        _this.LunBo.onmouseover = function () {
            clearInterval(_this.oImg.time);
            console.log(666);
        }
        //继续轮播
        _this.LunBo.onmouseout = function () {
            _this.selfMotion()
        }
    }


    
    //自动轮播
    LunBo.prototype.selfMotion = function () {
        this.oImg.time = setInterval(() => {
            this.rightBtn.onclick();
        }, 2000)
    }

    LunBo.prototype.fn = function () {
        let _this = this;
        for (let j = 0; j < _this.oBtn.length; j++) {
            _this.oBtn[j].className = '';
            // _this.oImg[j].style.opacity = 0;
            bufferMove(_this.oImg[j], { opacity: 0 });
        }
        _this.oBtn[_this.index].className = 'active';
        // _this.oImg[_this.index].style.opacity = 1;
        bufferMove(_this.oImg[_this.index], { opacity: 100 });
    }

    new LunBo().init();
</script>

</html>``

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值