萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

基於 jQuery 實現無縫向上滾動跑馬燈(公告列表)效果

效果描述

垂直列表自動向上滾動,第一項滾出可視區後移至列表底部,形成無限循環,類似股票跑馬燈、系統公告欄。

核心邏輯

  1. 容器 overflow: hidden 固定高度,只露出 N 行。
  2. ul 絕對定位或相對定位,通過 margin-top 動畫向上移動一行高度。
  3. 動畫完成後:margin-top 歸零,將第一個 li appendTo 列表末尾,實現無縫循環。

完整代碼實現

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery 無縫向上滾動跑馬燈</title>
  <style>
    .recordList {
      width: 300px; height: 100px; /* 顯示 5 行,每行 20px */
      border: 1px solid #ddd; overflow: hidden; position: relative;
      font-family: sans-serif; margin: 20px auto;
    }
    .tpl-rotate-recordList {
      position: absolute; top: 0; left: 0; margin: 0; padding: 0; list-style: none;
      transition: margin-top 0.5s ease; /* CSS 動畫更流暢 */
    }
    .tpl-rotate-recordList-item {
      height: 20px; line-height: 20px; padding: 0 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
  </style>
</head>
<body>
  <div class="recordList">
    <ul class="tpl-rotate-recordList">
      <li class="tpl-rotate-recordList-item">恭喜 187****5204 獲得 1000 RMB</li>
      <li class="tpl-rotate-recordList-item">恭喜 137****1372 獲得 1000 RMB</li>
      <li class="tpl-rotate-recordList-item">恭喜 156****0276 獲得 1000 RMB</li>
      <li class="tpl-rotate-recordList-item">恭喜 139****9856 獲得 1000 RMB</li>
      <li class="tpl-rotate-recordList-item">恭喜 136****0580 獲得 1000 RMB</li>
      <li class="tpl-rotate-recordList-item">恭喜 138****8118 獲得 1000 RMB</li>
    </ul>
  </div>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script>
    $(function() {
      var $list = $('.tpl-rotate-recordList');
      var $items = $list.children('li');
      var itemHeight = $items.outerHeight(); // 含 padding/border
      var visibleCount = Math.floor($list.height() / itemHeight);
      
      if ($items.length <= visibleCount) return; // 不足一屏不滾動

      var timer;
      function autoScroll() {
        // 使用 jQuery animate 配合 CSS transition 更平滑,這裡用 animate 演示邏輯
        $list.animate({ marginTop: -itemHeight }, 500, function() {
          // 動畫完成回調
          $list.css('marginTop', 0).find('li:first').appendTo(this); // 移動第一項到末尾
        });
      }

      // 啟動定時器
      timer = setInterval(autoScroll, 2000);

      // 鼠標懸停暫停
      $('.recordList').hover(
        function() { clearInterval(timer); },
        function() { timer = setInterval(autoScroll, 2000); }
      );
    });
  </script>
</body>
</html>

關鍵優化點

  1. 動態計算行高:outerHeight() 自適應 padding/border/line-height 變化。
  2. 項目數 ≤ 可視數不滾動:避免無意義動畫。
  3. 懸停暫停:提升用戶體驗,便於閱讀完整內容。
  4. CSS Transition 替代 jQuery animate:啟用 GPU 加速,性能更好。邏輯改為切換 margin-top class。
  5. 無縫銜接:appendTo 移動 DOM 節點而非複製,內存穩定。

現代純 CSS 實現(無 JS,效能最高)

<style>
  .marquee {
    height: 100px; overflow: hidden; position: relative;
  }
  .marquee ul {
    animation: scroll-up 10s linear infinite;
    /* 暫停懸停 */
  }
  .marquee:hover ul { animation-play-state: paused; }
  @keyframes scroll-up {
    0% { transform: translateY(0); }
    100% { transform: translateY(-100%); } /* 需列表內容複製一份拼接實現無縫 */
  }
</style>
CSS 動畫需將列表內容複製一份(ul.innerHTML += ul.innerHTML)才能實現真正無縫循環,否則會有跳變。

總結

jQuery 版通過 animate → marginTop 歸零 → appendTo 實現經典無縫滾動,邏輯清晰易控制(暫停、插入新項、點擊交互)。現代項目優先考慮 CSS Animation 或 IntersectionObserver 觸發,但理解 JS 版原理對於定製化交互(如點擊展開詳情、數據實時推送插入)仍有參考價值。

copyright © 萬盛學電腦網 all rights reserved