2013-03-02 32 views

回答

21

這裏的問題是,導航欄已被固定到頂端,導航欄下面的容器中的其他內容無法傳遞。你可以做到這一點使用更現代化的CSS,但要知道,這不會是舊版本瀏覽器修復(確實也有可能與現在的位置發現的問題:在舊的瀏覽器太固定屬性...

.affix + .container { 
    padding-top:50px 
} 

此等待,直到導航欄是固定的,然後添加填充到是它的兄弟姐妹,從「跳樓」保持它的導航下的容器

+0

感謝那些工作!例如,你的確是正確的,它在IE8上看起來很奇怪。要尋找一些哈克修復... – deekay 2013-03-03 10:39:19

+0

這工作在類似的問題,我有除了它是沒有bootstrap相關。謝啦! – Binary101010 2014-01-04 03:56:29

+0

不錯,謝謝 – dichterDichter 2015-01-06 18:10:11

4

我的解決方案,靈感來自@ niaccurshi的:

代替硬編碼一個填充頂部,創建一個不可見的重複元素,佔用相同數量的空間,但只有當原始元素被粘貼時。

JS:

var $submenu = $(".submenu"); 

// To account for the affixed submenu being pulled out of the content flow. 
var $placeholder = $submenu.clone().addClass("affix-placeholder"); 
$submenu.after($placeholder); 

$submenu.affix(…); 

CSS:

.affix-placeholder { 
    /* Doesn't take up space in the content flow initially. */ 
    display: none; 
} 

.affix + .affix-placeholder { 
    /* Once we affix the submenu, it *does* take up space in the content flow. But keep it invisible. */ 
    display: block; 
    visibility: hidden; 
} 
+2

這是一個很好的解決方案,雖然它可能是值得做的,你可以在重複的標題欄上設置ARIA屬性,以確保屏幕閱讀器忽略它(並且不要沒有通過你的標題欄兩次閱讀) – niaccurshi 2014-01-15 10:19:54

+0

@niaccurshi這是一個很好的觀點;謝謝。 – 2014-01-17 09:00:12

+0

我真的很喜歡這個。它避免需要找出所需的填充量 – Scribblemacher 2017-07-13 19:14:32

1

的選擇,如果你想避免重複的內容。

<script> 
    jQuery(document).ready(function(){ 
     jQuery("<div class='affix-placeholder'></div>").insertAfter(".submenu:last"); 
    }); 
</script> 

<style> 
.affix-placeholder { 
    display: none; 
} 
.affix + .affix-placeholder { 
    display: block; 
    visibility: hidden; 
    height: /* same as your affix element */; 
    width: 100%; 
    overflow: auto; 
} 
</style> 
4

您還可以使用引導貼上事件添加和移除填充(或保證金)從相關元素通過CSS類:

// add padding or margin when element is affixed 
$("#navbar").on("affix.bs.affix", function() { 
    return $("#main").addClass("padded"); 
}); 

// remove it when unaffixed 
$("#navbar").on("affix-top.bs.affix", function() { 
    return $("#main").removeClass("padded"); 
}); 

這裏有一個的jsfiddle:http://jsfiddle.net/mumcmujg/1/

+0

好的解決方案,我喜歡它。 – 2015-09-28 09:23:01

+0

這是一個可靠的解決方案,但我寧願考慮'affixed.bs.affix'和'affixed-top.bs.affix'事件,以便在每次轉換時只觸發一次。 – installero 2016-01-14 15:23:20