html - 让 Affix 为导航和导航栏工作

标签 html twitter-bootstrap navbar affix

我有一些 html 用于我的研究小组制作的软件库网站的页面布局。这是通过 bootstrap 3 完成的。我想要实现的是出现在 jumbotron 正下方的导航栏一旦向上滚动到页面顶部就会固定到页面顶部。我还希望页面左栏中的 nag 元素有类似的行为 - 当页面滚动并且它们到达顶部时 - 然后它们变得固定并停留在那里。通过阅读,我了解到推特 Bootstrap 中的 Affix 是执行此操作的方法,但我不完全确定我能想出如何正确执行此操作。

在下面的 HTML 中,滚动到现在应该横跨页面的导航栏被压扁 - 导航栏链接移动到导航栏品牌下方的一行,并且该栏不再横跨页面。一旦我弄清楚如何将词缀与导航栏一起使用,我希望将它与导航元素一起使用。谁能解释我需要做什么才能达到效果?我对网页设计相当陌生。

谢谢, 本。

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>

最佳答案

如 Bootstrap 文档中所述,当 affix 启动时,您必须为 nav 和 navbar 定义 CSS 类...

"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."

因此您需要为顶部导航栏和侧边导航栏添加一些 CSS。

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

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

这是一个使用 affix 和顶部和侧面导航的 Bootply 演示:http://bootply.com/69848

关于html - 让 Affix 为导航和导航栏工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450150/

相关文章:

javascript - phonegap 中的地理位置在模拟器上不起作用

html - 如何在图标上放置通知徽章?

css - Bootstrap 4 汉堡包图标在自定义后消失了

html - 使用 Bootstrap4 在我展开的切换按钮上留边距

html - Firefox 包装内联 div,但其他浏览器不包装

html - 过渡不适用于任何属性(property)?

html - 如何从系统驱动器将 src 给 html 中的 <img> 标签?

html - 错误显示下拉菜单(移动)bootstrap

html - 如何使按钮在 Bootstrap 中响应?

html - 使垂直导航栏超出其内容