轮播图...

<!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>
        #img {
            width: 500px;
            height: 400px;
            margin: 100px auto;
            position: relative;
            background-size: 500px 400px;
            transition: 1s all linear;
        }

        #right {
            width: 30px;
            height: 60px;
            position: absolute;
            top: 180px;
        }

        #left {
            width: 30px;
            height: 60px;
            position: absolute;
            top: 180px;
            left: 470px;
        }

        ul {
            display: flex;
            position: absolute;
            top: 300px;
            left: 150px;

        }

        li {
            list-style: none;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: black;
            margin-left: 10px;
            background-image: url(../img/);

        }
    </style>
</head>

<body>
    <div id="img">
        <input type="button" value="<" id="right">
        <input type="button" value=">" id="left">
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>

</html>
<script src="./轮播图带定时器.js"></script>
<script>
    let oImg = document.getElementById("img")
    let oRight = document.getElementById("right")
    let oLeft = document.getElementById("left")
    let oLi = document.getElementsByTagName("li")

    let a = new lunbo(oImg, oRight, oLeft, oLi)
</script>

class lunbo {
    constructor(img, right, left, li) {
        this.oImg = img;
        this.oRight = right;
        this.oLeft = left;
        this.oLi = li;
        this.index = 0;
        this.time=0;
        this.oImg.style.backgroundImage =`url(../img/${this.index}.jpg)`;
        this.oLi[this.index].style.backgroundColor = 'red';
        this.eventBind();

    }
    ipdate() {
        this.oImg.style.backgroundImage = `url(../img/${this.index}.jpg)`;
        for (let i = 0; i < this.oLi.length; i++) {
            if (i == this.index) {
                this.oLi[i].style.backgroundColor = 'red';
            } else {
                this.oLi[i].style.backgroundColor = 'black';
            }
        }
    }
    next() {
        this.index++;
        if (this.index == this.oLi.length) {
            this.index = 0;
        }
        this.ipdate();
    }

    prev() {
        this.index--;
        if (this.index == -1) {
            this.index = this.oLi.length - 1;
        }
        this.ipdate();

    }

    click() {
        let that = this
        for (let i = 0; i < this.oLi.length; i++) {
            this.oLi[i].onclick = function () {
                that.index = i;
                that.ipdate();

            }
        }
    }
    otime() {
        let that = this;
        clearInterval(this.time);
        this.time = setInterval(function () {
         that.next();
         that.click()  
        }, 1000)
    }

    stop() {
        clearInterval(this.time)
    }
    
    eventBind() {
        let that = this;
        this.oLeft.onclick = function () {
            that.next();
        }
        this.oRight.onclick = function () {
            that.prev();
        }
        this.click()
        this.otime()

        this.oImg.onmouseover = function () {
            that.stop()
        }

        this.oImg.onmouseout = function () {
            that.otime()
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值