scrollFix 是一款實用的滾動定位外掛。當使用者向下或向上滾動頁面到指定位置時,目標元素會自動切換為固定定位(position: fixed);當往回滾動離開該位置時,元素又會恢復原樣。你可以自訂觸發所需的相對螢幕位置與滾動方向,並相容 IE6。
$(".target_element").scrollFix( [ "top" | "bottom" | length(可為負,表示相對 bottom), [ "top" | "bottom" ] ]);
第一個參數:可選,預設為 "top",表示當目標元素到達螢幕的相對位置時開始觸發固定。可傳入數值,如 100、-200,負值代表相對於螢幕下方。
第二個參數:可選,預設為 "top",表示觸發固定所需的滾動方向。"top" 代表由上往下滾動時觸發,"bottom" 代表由下往上滾動時觸發。
<script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript" src="scrollFix.js"></script>
各組示範呼叫如下:
// 滾動到距離下方 200px 時開始固定,預設由上往下觸發
$("#a").scrollFix(-200);
// 滾動到距離上方 200px 時開始固定,指定 "bottom" 由下往上觸發
$("#b").scrollFix(200,"bottom");
// 滾動到距離上方 0 時開始固定,指定 "top" 由上往下觸發
$("#c").scrollFix("top","top");
// 滾動到距離下方 0 時開始固定,指定 "bottom" 由下往上觸發
$("#d").scrollFix("bottom","top");
頁面載入時的綁定寫法:
window.onload = function(){
$(".demo:eq(0)").scrollFix(-200);
$(".demo:eq(1)").scrollFix(200,"bottom");
$(".demo:eq(2)").scrollFix("top","top");
$(".demo:eq(3)").scrollFix("bottom","bottom");
}
;(function($) { jQuery.fn.scrollFix = function(height, dir) { height = height || 0; height = height == "top" ? 0 : height; return this.each(function() { if (height == "bottom") { height = document.documentElement.clientHeight - this.scrollHeight; } else if (height < 0) { height = document.documentElement.clientHeight - this.scrollHeight + height; } var that = $(this), oldHeight = false, p, r, l = that.offset().left; dir = dir == "bottom" ? dir : "top"; //默認滾動方向向下 if (window.XMLHttpRequest) { //非ie6用fixed function getHeight() { //>=0表示上面的滾動高度大於等於目標高度 return (document.documentElement.scrollTop || document.body.scrollTop) + height - that.offset().top; } $(window).scroll(function() { if (oldHeight === false) { if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) { oldHeight = that.offset().top - height; that.css({ position: "fixed", top: height, left: l }); } } else { if (dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) { that.css({ position: "static" }); oldHeight = false; } else if (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight) { that.css({ position: "static" }); oldHeight = false; } } }); } else { //for ie6 $(window).scroll(function() { if (oldHeight === false) { //恢復前只執行一次,減少reflow if ((getHeight() >= 0 && dir == "top") || (getHeight() <= 0 && dir == "bottom")) { oldHeight = that.offset().top - height; r = document.createElement("span"); p = that[0].parentNode; p.replaceChild(r, that[0]); document.body.appendChild(that[0]); that[0].style.position = "absolute"; } } else if ((dir == "top" && (document.documentElement.scrollTop || document.body.scrollTop) < oldHeight) || (dir == "bottom" && (document.documentElement.scrollTop || document.body.scrollTop) > oldHeight)) { //結束 that[0].style.position = "static"; p.replaceChild(that[0], r); r = null; oldHeight = false; } else { //滾動 that.css({ left: l, top: height + document.documentElement.scrollTop }) } }); } }); }; })(jQuery);
整體思路是:先依傳入的 height 換算出觸發門檻,非 IE6 環境直接用 fixed 定位;IE6 則改用 absolute 加上滾動補償,並以佔位 span 保留元素原本的文件流位置,避免滾動時發生回流抖動。