简体   繁体   English

引导菜单涵盖页面内容

[英]Bootstrap menu covering page content

I'd like to use a menu bar with Bootstrap that always stays on top of the page. 我想使用带有Bootstrap的菜单栏,该菜单栏始终位于页面顶部。 Therefore I used the class navbar-fixed-top . 因此,我使用了navbar-fixed-top I used the code from the very end of this post and ran into a problem with visibility of my content 我使用了本文结尾处的代码,但遇到了内容可见性问题

The main content is partly overlapped by the menue. 菜单部分重叠了主要内容。

在此处输入图片说明

On http://getbootstrap.com/examples/starter-template/ the problem is "solved" by adding padding-top: 50px; http://getbootstrap.com/examples/starter-template/上 ,通过添加padding-top: 50px;来“解决”问题padding-top: 50px; to the body. 到身体。 But this does not work, when the menu is wrapping and becomes larger than 50px: 但这在菜单包装并且变得大于50px时不起作用:

在此处输入图片说明

Is there a class in bootstrap that covers this problem? 引导程序中是否有涵盖该问题的类? Or do I have to add a JavaScript that listens to changes of the window-size and adjusts the padding-top? 还是我必须添加一个JavaScript来监听窗口大小的变化并调整padding-top? I have no idea how to do that, but that's the only thing that came to my mind,... 我不知道该怎么做,但这是我唯一想到的事情,...

CODE: 码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Bootstrap</title>
    <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
    <style>
        h1 {
            font-size: 5em;
        }
        body {
            /*
            padding-top: 50px;
            */
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="">Project name</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li><a href="#item01">Item01</a></li>
                    <li><a href="#item02">Item02</a></li>
                    <li><a href="#item03">Item03</a></li>
                    <li><a href="#item04">Item04</a></li>
                    <li><a href="#item05">Item05</a></li>
                    <li><a href="#item06">Item06</a></li>
                    <li><a href="#item07">Item07</a></li>
                    <li><a href="#item08">Item08</a></li>
                    <li><a href="#item09">Item09</a></li>
                    <li><a href="#item10">Item10</a></li>
                    <li><a href="#item11">Item11</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </nav>

    <div class="container"">
        <div>
            <h1 id="about">My first template</h1>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
            <p class="lead">Dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content, dummy content.</p>
        </div>
        <div id="contact" style="margin: 100em 0;">Contact us</div>
    </div><!-- /.container -->

    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <script>
        /* jQuery test. */
        $(function() {
            $('h1').html("Test");
        });
    </script>
</body>
</html>

I am unaware of a bootstrap class that fixes this problem. 我不知道可以解决此问题的引导程序类。 You could certainly add a listener to you window resize as you mentioned. 正如您所提到的,您当然可以在窗口大小调整中添加一个侦听器。 It would solve the problem of your height not always being 50px. 它将解决您的身高不总是50px的问题。 Should be something to the effect of: 应该具有以下效果:

$(window).on('resize', function(){
    var navHeight = $('nav').height();
    $('body').css('padding-top', navHeight + 'px');
});

This is untested, but should get you started. 这未经测试,但可以帮助您入门。 Hope this helps! 希望这可以帮助!

The thing is when you use "position: fixed" the element is taken out of the "flow" and is positioned relative to the window and not to the other elements. 问题是,当您使用“位置:固定”时,该元素将从“流”中取出,并且相对于窗口而不是相对于其他元素定位。 Therefore you must add a margin, like they have done in the bootstrap example, for the menu to not cover the underlying content. 因此,必须像在引导程序示例中所做的那样添加边距,以使菜单不覆盖基础内容。 You can also add padding to body but I would prefer using margin on the element instead. 您也可以向主体添加填充,但我宁愿在元素上使用空白。

If you don't know the height of the menu on forehand I would recommend you use the answer @wrxsti wrote. 如果您不知道正手菜单的高度,建议您使用@wrxsti编写的答案。

Give .navbar a margin-bottom within your CSS. 在CSS中为.navbar一个margin-bottom That should fix it. 那应该解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM