需要在向下滚动

时间:2018-04-30 05:23:48

标签: html css html5 twitter-bootstrap-3

我需要修改以下引导代码,以便在向下滚动时固定在网页顶部。怎么办?请帮帮我..

这是我的bootstrap代码。请在向下滚动网页时帮我将此代码从静态更改为固定。                                                       

     <div class="header_top" id="home">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <button class="navbar-toggler navbar-toggler-right mx-auto" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>


        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mx-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="index.html">Home
                        <span class="sr-only">(current)</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="about.html">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link scroll" href="#menu">Menu</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="blog.html">Blog</a>
                </li>
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true"
                        aria-expanded="false">
                        Drop Down
                    </a>
                    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#"></a>
                        <a class="dropdown-item" href="gallery.html">Gallery</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="404.html">Services</a>

                    </div>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="contact.html">Contact</a>
                </li>

            </ul>
        </div>
    </nav>

</div>

3 个答案:

答案 0 :(得分:0)

<nav class="navbar navbar-default navbar-fixed-top">

    ...   

阅读本文档了解更多详情 https://getbootstrap.com/docs/3.3/components/#navbar-fixed-top

答案 1 :(得分:0)

你可以使用css:

nav{
    position: fixed;
}

实际上是在课程navbar-fixed-top

背后写的

答案 2 :(得分:0)

您可以添加:<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">

data-spy="affix" data-offset-fop="scroll amount"

您可以看到此链接,例如: https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_affix&stacked=h

另一个例子:https://www.codeply.com/go/LDHQhWUG0x