Question

I have some html for a layout for a page for a site for software library my research group made. This is done with bootstrap 3. What I would like to achieve is the navbar that occurs just below the jumbotron to become fixed to the top of the page once it get's scrolled up there. I'd also like similar behaviour for the nag element in the left column of the page - when the page is scrolled and they reach the top - that they then become fixed and stay there. From reading I understand that Affix in twitter bootstrap is the way to go about doing this, but I'm not entirely sure I can figure how to do this right.

With the HTML below what happens is after scrolling so far the navbar which should span the page becomes squashed - the navbar links move to one row below the navbar brand and the bar nolonger spans the page. Once I figure out how to use affix with the navbar I hope to use it with the nav element. Can anyone explain what I need to do to get the effect? I'm fairly new to web design stuff.

Thanks, Ben.

HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>SOFTWARE - GETTING STARTED WITH SOFTWARE</title> 
        <link rel="stylesheet" href="css/bootstrap.css" type="text/css"/>
    </head>
    <body>
        <style>
            .jumbotron {
                margin-bottom: 0px;
            }
        </style>
        <!-- Start of main container -->
        <div class="container-fluid main">
            <div class="row-fluid">
                <div class="jumbotron">
                    <h1> Getting Started </h1>
                    <p> Installing SOFTWARE on your machine is easy for most systems.
                        This guide will get you up and running.
                    </p>
                </div>
            </div>
                    <!--Here's thestart of the navabar I'm trying to affix -->
            <div class="row-fluid" data-spy="affix" data-offset-top="200">
                <div class="navbar navbar-inverse">
                    <div class="navbar-header">
                        <a class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </a>
                        <a class="navbar-brand" href="#">SOFTWARE</a>
                    </div>
                    <div class="navbar-collapse collapse">
                        <ul class="nav navbar-nav">
                            <li><a href="#">Home</a></li>
                            <li><a href="#">About SOFTWARE</a></li>
                            <li class="active"><a href="#">Getting Started</a></li>
                            <li><a href="#">Quick Start</a></li>
                            <li><a href="#">Full Documentation</a></li>
                            <li><a href="#">Contact</a></li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Download <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#">GitHub Repository</a></li>
                                    <li><a href="#">Download TAR</a></li>
                                    <li><a href="#">Download ZIP</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </div>  
            </div>
            <!-- Begin Layout for Getting Started Page -->
            <div class="row-fluid">
                <div class="col-lg-3">
                    <ul class="nav nav-pills nav-stacked sidemenu">
                        <li><a href="#">Introduction</a></li>
                        <li><a href="#">Installing R</a></li>
                        <li><a href="#">R Recommendations</a></li>
                        <li><a href="#">Installing HybRIDS from GitHub</a></li>
                        <li><a href="#">Installing HybRIDS from Source File</a></li>
                        <li><a href="#">Installing HybRIDS from Binary File</a></li>
                    </ul>
                </div>
                <div class="col-lg-9 whitep">
                    <h1 class="whitep">Introduction</h1>
                    <p>
                        Installation text
                        Installation text
                        Installation text
                        Installation text
                        Installation text
                    </p>
                    <hr>
                    <h1>Installing R</h1>
                    <p>
                        R is simple to install for most operating systems. If you use a Mac or PC or Linux system, the process for installing it is like for installing any other software on that system. 
                    </p>
                    <p>
                    TEST text to fill page to test scrolling behaviour...
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>
                    <p>
                    TEST
                    </p>    
                </div>
            </div>
        </div> <!--End of main Container-->
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="js/bootstrap.js"></script>
    </body>
</html>
Was it helpful?

Solution

As stated in the Bootstrap docs, you have to define the CSS classes for nav and navbar for when the affix kicks in...

"You must provide the styles for these classes yourself (independent of this plugin). The affix-top class should be in the regular flow of the document."

So you'll need to have some CSS for both the top navbar and side nav..

#nav.affix {
    position: fixed;
    top: 0;
    width: 100%;
    z-index:10;
}

#sidebar.affix {
    position: fixed;
    top: 80px;
}

Here is a Bootply demo using affix with both top and side nav: http://bootply.com/69848

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top