2012-12-07 38 views
1

我想要做的是從窗口頂部起1​​0px的固定導航欄,除非它位於文檔的第一個200像素內,那麼我希望它距離文檔的第一個200像素頂部...具有兩種不同狀態的固定導航欄

所以基本上我想要一個從頂部200px的導航欄開始,但是當用戶向下滾動190px導航欄滾動時,始終保持窗口頂部10px。

+0

[這常常與頭做](http://css-tricks.com/persistent-headers/)。 – zzzzBov

回答

3

您首先收聽窗口的滾動事件,然後使用滾動值來知道要應用於您的元素的狀態。實施例用jQuery:

var fixed = false, limit = 50; 
$(window).scroll(function() 
{ 
    if (window.scrollTop < 50 && fixed) 
    { 
     $("#header").css({ position: "relative" }); 
     fixed = false; 
    } 
    else if (window.scrollTop > 50 && !fixed) 
    { 
     $("#header").css({ position: "fixed" }); 
     fixed = true; 
    } 
}); 

Also related to this post for code example

+1

要是因爲我用JScrollPane中進行一些修改... \t'$( '#特容器')。綁定( 'JSP滾動-Y',函數(事件,scrollPositionY){ \t如果(scrollPositionY < (scrollRositionY> = 190) \t $(「#navigation」)。css({position:'absolute',top:「200px」}); \t else if(scrollPositionY> = 190) ({position:'fixed',top:「10px」}); \t});' –