DIV CSS 佈局教程網

Follow.js:多目標跟隨捲動外掛
編輯:JavaScript綜合知識     

側邊欄的推薦連結、廣告、QR Code——希望它們在使用者捲動時固定跟在旁邊,但又不希望壓到頁尾。這個外掛就是處理這件事。

功能特點

一、支援同時定義多個跟隨物件,用 CSS position: fixed 實作,不支援 IE6,相容其他主流瀏覽器。
二、可設定捲到頁面底部時的最小間隔高度,跟隨區塊不會覆蓋頁尾內容。
三、視窗 resize 之後自動重設參數,不必手動重新計算位置。

第二點是這個外掛最有價值的地方。單純用 position: fixed 會有個惱人問題:捲到頁面最底部時,固定元素會壓在頁尾資訊上。加入底部間隔判斷之後,接近頁尾時它會停止跟隨,讓頁尾正常呈現。

使用方法

<script type="text/javascript" src="follow.js"></script>

<script>
window.onload = function () {
  var followIds = [
    document.getElementById("follow"),
    document.getElementById("follow2")
  ];
  new Follow({
    obj: followIds,   // 要跟隨的元素陣列
    bottom: 150       // 距頁面底部的最小高度
  });
};
</script>

三個步驟:載入外掛 → 建立要跟隨的元素陣列 → 初始化並傳入參數。

完整範例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Js Follow</title>
<style>
html, body { width:100%; margin:0; padding:0; }
div { padding:0; margin:0; text-align:center; font-size:40px;
      font-weight:bold; color:#fff; }
.clear:after { content:""; display:block; height:0; clear:both;
               visibility:hidden; overflow:hidden; }
.clear { *zoom:1; }

.wrap    { width:1000px; margin:0 auto; }
.header  { width:100%; background:#1BA1E2; height:200px; line-height:300px; }
.footer  { width:100%; background:#666;    height:150px; line-height:100px; }
.content { width:100%; margin:0 auto; background:#aaa; }
.left    { width:70%; float:left; background:#8CBF26; }
.text    { padding:50px; text-align:left; word-break:break-all;
           line-height:36px; font-size:16px; font-weight:normal; }
.right   { width:30%; float:left; }
.aside   { padding:10px 0; margin-top:50px; background:#F09609;
           width:300px; height:150px; }
.aside2  { padding:10px 0; background:#DB4F33; }
</style>
</head>
<body>
  <div class="wrap">
    <div class="header">Header</div>
    <div class="content clear">
      <div class="left">
        <div class="text">...大量內容,讓頁面夠長以便捲動...</div>
      </div>
      <div class="right">
        <div class="aside"  id="follow">跟隨捲動 模組1</div>
        <div class="aside2" id="follow2">跟隨捲動 模組2</div>
      </div>
    </div>
    <div class="footer">Footer</div>
  </div>

  <script type="text/javascript" src="follow.min.js"></script>
  <script>
  window.onload = function () {
    var followIds = [document.getElementById("follow"),
                     document.getElementById("follow2")];
    new Follow({ obj: followIds, bottom: 150 });
  };
  </script>
</body>
</html>

版面刻意用 .clear:after 清除浮動,讓 .content 正確包住左右兩欄;若不清除,後面的 footer 會跑到浮動元素的旁邊,整個版面就崩了。

幾個使用上的注意

為什麼要用 window.onload:外掛需要讀取元素的實際尺寸與位置,這些值必須在版面配置完成後才正確。若在 DOM 尚未就緒時初始化,會拿到 0 而計算錯誤。用 DOMContentLoaded 通常也行,但 onload 最保險。

bottom: 150 要對應頁尾高度:設太小會壓到頁尾,設太大則跟隨區塊會提早停止移動。建議抓 footer 實際高度再加一點餘裕。

IE6 不支援是因為 position: fixed:IE6 完全不認這個屬性值,這是瀏覽器本身的限制,不是外掛的問題。

今天怎麼做更省事

現代 CSS 有 position: sticky,一行就能完成大部分跟隨效果,而且效能遠優於用 JavaScript 在 scroll 事件中不斷改 style:

.aside {
  position: sticky;
  top: 20px;        /* 距視窗頂部的間隔 */
}

它的運作方式是:元素在容器範圍內會保持相對定位,捲到 top 指定的位置時就固定住,而當容器(例如側邊欄)的底邊即將離開視窗時,它會自然跟著離開——「不覆蓋頁尾」這個需求它天生就解決了,不需要 bottom 參數,也不必監聽 scroll 與 resize 事件。

若需要更複雜的行為(例如依捲動深度改變樣式),再加 IntersectionObserver 搭配即可,效能也比 scroll 事件好得多。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved