You are viewing an old revision of this post, from September 21, 2012 @ 14:58:58. See below for differences between this version and the current revision.

Add subnav component and behavior to Bootstrap

Taken from GitHub here. I just spent an hour figuring this out so here's how I did it. Please chip in if I'm doing something that is not necessary. Make sure that the link to the docs.css file is in the <head> of your document like this. The file that I'm using the subnav in is in the root of my site hence the path.
<link href="../docs/assets/css/docs.css" rel="stylesheet"> 
Make sure the <body> looks like this
<body data-spy="scroll" data-target=".subnav" data-offset="50"> 
data-spy is for scrollspy but I leave it in as it's sweet. You will also need the following at the end of the document, but providing you are using one on the bootstrap templates you should be fine.
<script src="/docs/assets/js/application.js"></script> 
And that's it. It should work. The only problem with this approach is you get the squared background image and your logo moves to the right in the navbar but you can just comment that out in docs.css Hope that helps. Then some smart alec came up with this Javascript.....whoi knows if it works? [js] // fix sub nav on scroll var $win = $(window) , $nav = $('.subnav') , navTop = $('.subnav').length && $('.subnav').offset().top - 40 , isFixed = 0 processScroll() $win.on('scroll', processScroll) function processScroll() { var i, scrollTop = $win.scrollTop() if (scrollTop >= navTop && !isFixed) { isFixed = 1 $nav.addClass('subnav-fixed') } else if (scrollTop <= navTop && isFixed) { isFixed = 0 $nav.removeClass('subnav-fixed') } }[/js]

Revisions

  • September 21, 2012 @ 14:59:24 [Current Revision] by PeterLugg
  • September 21, 2012 @ 14:58:58 by PeterLugg
  • September 21, 2012 @ 14:57:02 by PeterLugg

Revision Differences

September 21, 2012 @ 14:58:58Current Revision
Content
 Added: Taken from GitHub <a href="https:/ /github.com/twitter/bootstrap/ issues/1189" target="_blank">here</a>.
Unchanged: I just spent an hour figuring this out so here's how I did it. Please chip in if I'm doing something that is not necessary.Unchanged: I just spent an hour figuring this out so here's how I did it. Please chip in if I'm doing something that is not necessary.
Unchanged: Make sure that the link to the docs.css file is in the <code>&lt; head&gt;</code> of your document like this. The file that I'm using the subnav in is in the root of my site hence the path.Unchanged: Make sure that the link to the docs.css file is in the <code>&lt; head&gt;</code> of your document like this. The file that I'm using the subnav in is in the root of my site hence the path.
Unchanged: <pre><code>&lt;link href="../docs/ assets/css/docs.css" rel="stylesheet"&gt; </code></pre>Unchanged: <pre><code>&lt;link href="../docs/ assets/css/docs.css" rel="stylesheet"&gt; </code></pre>
Unchanged: Make sure the <code>&lt; body&gt;</code> looks like thisUnchanged: Make sure the <code>&lt; body&gt;</code> looks like this
Unchanged: <pre><code>&lt;body data-spy="scroll" data-target=".subnav" data-offset="50"&gt; </code></pre>Unchanged: <pre><code>&lt;body data-spy="scroll" data-target=".subnav" data-offset="50"&gt; </code></pre>
Unchanged: data-spy is for scrollspy but I leave it in as it's sweet.Unchanged: data-spy is for scrollspy but I leave it in as it's sweet.
Unchanged: You will also need the following at the end of the document, but providing you are using one on the bootstrap templates you should be fine.Unchanged: You will also need the following at the end of the document, but providing you are using one on the bootstrap templates you should be fine.
Unchanged: <pre><code>&lt;script src="/docs/assets/ js/application.js" &gt;&lt;/script&gt; </code></pre>Unchanged: <pre><code>&lt;script src="/docs/assets/ js/application.js" &gt;&lt;/script&gt; </code></pre>
Unchanged: And that's it. It should work. The only problem with this approach is you get the squared background image and your logo moves to the right in the navbar but you can just comment that out in docs.cssUnchanged: And that's it. It should work. The only problem with this approach is you get the squared background image and your logo moves to the right in the navbar but you can just comment that out in docs.css
Unchanged: Hope that helps.Unchanged: Hope that helps.
Unchanged: Then some smart alec came up with this Javascript.....whoi knows if it works?Unchanged: Then some smart alec came up with this Javascript.....whoi knows if it works?
Unchanged: [js] // fix sub nav on scrollUnchanged: [js] // fix sub nav on scroll
Unchanged: var $win = $(window)Unchanged: var $win = $(window)
Unchanged: , $nav = $('.subnav')Unchanged: , $nav = $('.subnav')
Unchanged: , navTop = $('.subnav').length &amp;&amp; $('.subnav').offset().top - 40Unchanged: , navTop = $('.subnav').length &amp;&amp; $('.subnav').offset().top - 40
Unchanged: , isFixed = 0Unchanged: , isFixed = 0
Unchanged: processScroll()Unchanged: processScroll()
Unchanged: $win.on('scroll', processScroll)Unchanged: $win.on('scroll', processScroll)
Unchanged: function processScroll() {Unchanged: function processScroll() {
Unchanged: var i, scrollTop = $win.scrollTop()Unchanged: var i, scrollTop = $win.scrollTop()
Unchanged: if (scrollTop &gt;= navTop &amp;&amp; !isFixed) {Unchanged: if (scrollTop &gt;= navTop &amp;&amp; !isFixed) {
Unchanged: isFixed = 1Unchanged: isFixed = 1
Unchanged: $nav.addClass( 'subnav-fixed')Unchanged: $nav.addClass( 'subnav-fixed')
Unchanged: } else if (scrollTop &lt;= navTop &amp;&amp; isFixed) {Unchanged: } else if (scrollTop &lt;= navTop &amp;&amp; isFixed) {
Unchanged: isFixed = 0Unchanged: isFixed = 0
Unchanged: $nav.removeClass( 'subnav-fixed')Unchanged: $nav.removeClass( 'subnav-fixed')
Unchanged: }Unchanged: }
Unchanged: }[/js]Unchanged: }[/js]

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply