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>
相关推荐
Myli_ing9 分钟前
考研倒计时-配色+1
前端·javascript·考研
余道各努力,千里自同风12 分钟前
前端 vue 如何区分开发环境
前端·javascript·vue.js
PandaCave19 分钟前
vue工程运行、构建、引用环境参数学习记录
javascript·vue.js·学习
软件小伟21 分钟前
Vue3+element-plus 实现中英文切换(Vue-i18n组件的使用)
前端·javascript·vue.js
醉の虾42 分钟前
Vue3 使用v-for 渲染列表数据后更新
前端·javascript·vue.js
张小小大智慧1 小时前
TypeScript 的发展与基本语法
前端·javascript·typescript
hummhumm1 小时前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
asleep7011 小时前
第8章利用CSS制作导航菜单
前端·css
hummhumm1 小时前
第 28 章 - Go语言 Web 开发入门
java·开发语言·前端·python·sql·golang·前端框架
幼儿园的小霸王2 小时前
通过socket设置版本更新提示
前端·vue.js·webpack·typescript·前端框架·anti-design-vue