Logo垂直跑马灯循环结束后出现跳转问题求助
垂直Logo跑马灯循环跳转问题修复方案
问题核心
你的跑马灯在循环结束后出现突然跳转,本质是滚动复位逻辑错误,加上手动高度计算的误差,导致克隆元素无法无缝衔接原列表的滚动轨迹。
问题分析
- 复位逻辑错误:当滚动到原列表底部时,直接将
topValue重置为0,此时克隆的元素会被瞬间替换为原列表,视觉上产生跳变。 - 高度计算冗余:手动计算
totalHeight时重复叠加了gap值,flex容器的gap已经包含在列表高度中,额外计算会导致滚动临界点判断偏差。
修复后的完整代码
<!DOCTYPE html> <html> <head> <title>Vertical Logo Marquee</title> <style> .marquee-container { height: 200px; overflow: hidden; position: relative; } .marquee-list { position: absolute; top: 0; width: 100%; height: auto; list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; } .marquee-list li { height: 150px; display: flex; align-items: center; background-color: red; } .marquee-list li img { max-height: 100%; max-width: 100%; margin: 0 auto; } </style> </head> <body> <div class="marquee-container"> <ul class="marquee-list"> <li><img src="logo1.png" alt="Logo 1"></li> <li><img src="logo2.png" alt="Logo 2"></li> <li><img src="logo3.png" alt="Logo 3"></li> <li><img src="logo4.png" alt="Logo 4"></li> <li><img src="logo5.png" alt="Logo 5"></li> </ul> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function () { var marqueeContainer = $(".marquee-container"); var marqueeList = $(".marquee-list"); var marqueeItems = $(".marquee-list li"); // 克隆前直接获取原列表总高度(包含flex gap) var totalHeight = marqueeList.height(); var topValue = 0; // 克隆元素实现循环衔接 marqueeItems.clone().appendTo(marqueeList); function moveMarquee() { topValue -= 1; // 当原列表完全滚出容器时,叠加原高度实现无缝衔接 if (topValue < -totalHeight) { topValue += totalHeight; } marqueeList.css("top", topValue + "px"); } var marqueeInterval = setInterval(moveMarquee, 20); // 鼠标悬停暂停/恢复滚动 marqueeContainer.on("mouseenter", function () { clearInterval(marqueeInterval); }).on("mouseleave", function () { marqueeInterval = setInterval(moveMarquee, 20); }); }); </script> </body> </html>
关键修改点
- 滚动复位逻辑:将
topValue = 0改为topValue += totalHeight,原列表完全滚出后,克隆列表会无缝接档,避免跳转。 - 高度计算优化:直接用
marqueeList.height()获取原列表总高度(自动包含flex gap),消除手动计算的误差。 - 样式单位规范:设置
top属性时明确添加px单位,确保样式解析准确。
内容的提问来源于stack exchange,提问作者Karim Essam
相关产品推荐
相关产品推荐

