js轮播原生

<!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>
        * {
            margin: 0%;
            padding: 0%;
            list-style: none;
        }

        .max {
            width: 1400px;
            height: 515px;
            margin: auto;
            position: relative;
            overflow: hidden;
        }

        ul {
            display: flex;
            width: 8400px;
            /* width: 7000px; */
            position: absolute;
            left: 0px;
            /* height: 730px; */
        }

        ul li {
            width: 1400px;
        }

        img {
            width: 100%;
        }

        .min {
            position: absolute;
            bottom: 8px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
        }

        .max .min>div {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            border: 1px solid rgb(19, 16, 16);
            margin: 0px 10px;
        }

        .blu {
            background-color: blue;
        }
    </style>
</head>

<body>
    <div class="max">
        <ul>
            <li><img src="./26623103960ec0e55846c7.jpg" alt=""></li>
            <li><img src="./46565237261d50f4269719.jpg" alt=""></li>
            <li><img src="./51894802361d50eeb12316.jpg" alt=""></li>
            <li><img src="./51912058060a32c55f0d23.jpg" alt=""></li>
            <li><img src="./91990417062a143b9b51d3.jpg" alt=""></li>
            <li><img src="./26623103960ec0e55846c7.jpg" alt=""></li>
        </ul>
        <div class="min">
            <div class="blu"></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <button>上</button>
    <button>下</button>
    <div></div>
</body>

</html>
<script>
    var btn = document.querySelectorAll('button');
    var uls = document.querySelector('ul');
    var max = document.querySelector('.max');
    var divs = document.querySelectorAll('.min>div');
    var index = 0;
    var b = true;

    function scroll(flag) {
        b = false;
        if (index == uls.children.length - 1) {
            index = 0;
            uls.style.left = 0 + 'px';
        }
        if (flag) {
            index++;
        } else {
            index--;
        }
        if (index < 0) {
            index = 4;
            uls.style.left = -7000 + 'px';
        }
        for (var i = 0; i < divs.length; i++) {
            divs[i].className = '';
        }
        if (index == 5) {
            divs[0].className = 'blu';
        } else {
            divs[index].className = 'blu';
        }
        console.log(index);

        animation(uls, -index * uls.children[0].offsetWidth, flag);
    }
    // var flag = false;
    function animation(obj, target, flag) {
        clearInterval()
        obj.time = setInterval(function () {
            var cur = obj.offsetLeft;//获取当前元素的off
            //判断滚动方向
            if (flag) {
                cur -= 25;
            } else {
                cur += 25;
            }
            if (flag ? cur > target : cur < target) {
                obj.style.left = cur + 'px';
            } else {
                b = true;
                clearInterval(obj.time);
                obj.style.left = target + 'px';
            }
        }, 10)
    }


    btn[1].addEventListener('click', function () {
        if (b) {
            scroll(true);
        }
    });
    btn[0].addEventListener('click', function () {
        if (b) {
            scroll(false);
        }
    })
    // uls.time = setInterval(function () {
    //     if (b) {
    //         scroll(true);
    //     }
    // }, 2000)

    function zidong() {
        if (b) {
            scroll(true);
        }
    }
    //鼠标以上清除定时器
    var test = setInterval(zidong, 2000)
    max.addEventListener('mousemove', function () {
        clearInterval(test);
        this.style.cursor = 'pointer';
    })
    max.addEventListener('mouseout', function () {
        test = setInterval(zidong, 2000);
    })

    //小圆点
    for (var j = 0; j < divs.length; j++) {
        divs[j].setAttribute('qq', j);

        divs[j].addEventListener('click', function () {
            for (var k = 0; k < divs.length; k++) {
                divs[k].className = '';
            }
            var qq = this.getAttribute('qq');
            console.log(qq);
            index = qq - 1;
            if (b) {
                scroll(true);
            }
            this.className = 'blu';
        })
    }
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值