js实现上下无缝滚动(不卡顿)

效果图如下:


代码如下:
html 复制代码
<!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>
        * {
            padding: 0;
            margin: 0;
        }

        .out-box {
            margin: 100px auto;
            width: 600px;
            height: 300px;
            border: 1px solid #f29634;
            overflow: hidden;
        }

        .list .item {
            width: 500px;
        }

        .list .item {
            margin-bottom: 30px;
        }

        .list .item .top {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .top .top-left {
            flex: 1;
        }

        .top .top-left .num {
            color: #f29634;
            font-size: 20px;
            padding-right: 10px;
        }

        .bottom {
            position: relative;
            width: 100%;
            height: 2px;
            background: #f29634;
            margin-top: 20px;
        }

        .bottom::after {
            position: absolute;
            content: "";
            width: 80%;
            height: 10px;
            bottom: 5px;
            background: #f29634;
        }
    </style>
</head>

<body>
    <div class="out-box" id="demo">
        <div class="list" id="demo1">
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.1</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.2</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.3</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.4</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.5</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
            <div class="item">
                <div class="top">
                    <div class="top-left">
                        <span class="num">No.6</span>
                        <span>宝娟</span>
                    </div>
                    <div class="top-right">
                        我嗓子痛
                    </div>

                </div>
                <div class="bottom"></div>
            </div>
        </div>
        <div class="list" id="demo2"></div>
    </div>
    <script>
        var speed = 10
        var _demo = document.getElementById("demo");
        var _demo1 = document.getElementById("demo1");
        var _demo2 = document.getElementById("demo2");
        demo2.innerHTML = demo1.innerHTML
        function Marquee() {
           
            if (_demo2.offsetTop - _demo.scrollTop-_demo.offsetTop <= 0) {
                // _demo.scrollTop -= (_demo1.offsetHeight + (_demo1.offsetHeight - _demo.offsetHeight) )
                _demo.scrollTop = 0
                console.log('<0:'+ _demo.scrollTop);
            } else {
                _demo.scrollTop++
                // console.log('>0:'+ _demo.scrollTop);
            }
        }
        var MyMar = setInterval(Marquee, speed)
        _demo.onmouseover = function () { clearInterval(MyMar) }
        _demo.onmouseout = function () { MyMar = setInterval(Marquee, speed) }
    </script>
</body>

</html>
相关推荐
GIS程序媛—椰子13 分钟前
【Vue 全家桶】7、Vue UI组件库(更新中)
前端·vue.js
DogEgg_00119 分钟前
前端八股文(一)HTML 持续更新中。。。
前端·html
ZL不懂前端22 分钟前
Content Security Policy (CSP)
前端·javascript·面试
乐闻x25 分钟前
ESLint 使用教程(一):从零配置 ESLint
javascript·eslint
木舟100926 分钟前
ffmpeg重复回听音频流,时长叠加问题
前端
王大锤439136 分钟前
golang通用后台管理系统07(后台与若依前端对接)
开发语言·前端·golang
我血条子呢1 小时前
[Vue]防止路由重复跳转
前端·javascript·vue.js
黎金安1 小时前
前端第二次作业
前端·css·css3
啦啦右一1 小时前
前端 | MYTED单篇TED词汇学习功能优化
前端·学习