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>列表滚动</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100vw;
            height: 100%;
            display: table-cell;
        }

        ul {
            list-style: none;
            position: absolute;
            width: 100%;
        }

        ul>li {
            line-height: 60px;
            border: 1px solid rgba(0, 0, 0, .2);
            text-indent: 2em;
        }

        .container {
            width: 200px;
            height: 300px;
            border: 1px solid rgba(0, 0, 0, .2);
            border-radius: 6px;
            margin: 50px auto;
            overflow: hidden;
            position: relative;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul>
            <li>ul下的li1</li>
            <li>ul下的li2</li>
            <li>ul下的li3</li>
            <li>ul下的li4</li>
            <li>ul下的li5</li>
            <li>ul下的li6</li>
        </ul>
    </div>
    <script>
        window.onload = function () {
            const ul = document.querySelector("ul")
            const ulList = ul.getBoundingClientRect()
            console.log(ulList);
            ul.innerHTML += ul.innerHTML
            let t = 0
            let taskId = 0
            timeTask()
            function timeTask() {
                taskId = setInterval(() => {
                    ul.style.top = `-${t += 0.1}px`
                    if (t >= ulList.height) {
                        t = 0
                    }
                }, 1);
            }
            function stopTopMove() {
                clearInterval(taskId)
            }
            ul.addEventListener('mouseenter', stopTopMove)
            ul.addEventListener('mouseleave', timeTask)
        }
    </script>
</body>

</html>
posted @ 2022-05-05 17:47  走我们钓鱼去  阅读(218)  评论(0)    收藏  举报