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>
相关推荐
旧识君22 分钟前
移动端1px终极解决方案:Sass混合宏工程化实践
开发语言·前端·javascript·前端框架·less·sass·scss
ElasticPDF-新国产PDF编辑器1 小时前
Angular use pdf.js and Elasticpdf tutorial
javascript·pdf·angular.js
吃没吃1 小时前
vue2.6-源码学习-Vue 核心入口文件分析
前端
Carlos_sam1 小时前
Openlayers:海量图形渲染之图片渲染
前端·javascript
XH2761 小时前
Android Retrofit用法详解
前端
鸭梨大大大1 小时前
Spring Web MVC入门
前端·spring·mvc
你的人类朋友1 小时前
MQTT协议是用来做什么的?此协议常用的概念有哪些?
javascript·后端·node.js
吃没吃1 小时前
vue2.6-源码学习-Vue 初始化流程分析 (src/core/instance/init.js)
前端
XH2761 小时前
Android Room用法详解
前端
木木黄木木2 小时前
css炫酷的3D水波纹文字效果实现详解
前端·css·3d