简体   繁体   English

将Bootstrap导航栏固定到动态宽度DIV的顶部-并非页面的全宽

[英]Fixed Bootstrap navbar to top of dynamic width DIV - not full width of page

My problem 我的问题

I am using Bootstrap 3.0 and need to use a fixed navbar, which will stay at the top of a DIV on the left side and NOT extend all the way 100% of the width of the window (like I usually see Bootstrap's example do). 我正在使用Bootstrap 3.0,并且需要使用固定的导航栏,该导航栏将位于左侧DIV的顶部,并且不会一直延伸到窗口宽度的100%(就像我通常看到的Bootstrap的示例一样)。 I have some very simple code below, using ASP.NET MVC 4 which will explain the RenderSection areas. 我下面有一些使用ASP.NET MVC 4的非常简单的代码,它将解释RenderSection区域。

I have included an image which shows what I am trying to do. 我已包含一张图像,该图像显示了我要执行的操作。 Currently the navbar is only extending about 75% of the left side container, not the full 100% of that left side container like I want it to. 目前,导航栏仅扩展了左侧容器的大约75%,而不是像我想要的那样完全扩展了左侧容器的100%。

Like I said above, I want the navbar to be fixed so when you scroll it stays in one spot within that left DIV. 就像我在上面说的那样,我希望导航栏是固定的,以便在滚动时它保持在左DIV的一个位置。

Any tips/hints would be greatly appreciated! 任何提示/提示将不胜感激! Thanks! 谢谢!

I also saw this question ( Navbar fixed to top but not full-width ) but I do not want to have to dynamically maintain this navbar with a fixed pixel width in media queries. 我也看到了这个问题(导航栏固定在顶部,但不是全角 ),但我不想在媒体查询中以固定的像素宽度动态地维护此导航栏。 I'd prefer it to fill out the entire left side DIV that contains it. 我希望它填写包含它的整个左侧DIV。

概述我的问题的图像 ( http://i.imgur.com/YLzmkHp.jpg ) http://i.imgur.com/YLzmkHp.jpg

My code 我的密码

<div class="container">
    <div class="row">
        <!--LEFT SIDE-->
        <div class="col-md-9">
            <div data-spy="affix" data-offset-top="0">
            <nav class="navbar navbar-default" role="navigation">
                <div class="collapse navbar-collapse navbar-ex1-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Link</a></li>
                        <li><a href="#">Link</a></li>
                        <li class="dropdown">
                            <a data-toggle="dropdown" class="dropdown-toggle" href="#">Dropdown <b class="caret"></b></a>
                            <ul role="menu" class="dropdown-menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li><a href="#">Separated link</a></li>
                                <li><a href="#">One more separated link</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form role="search" class="navbar-form navbar-left">
                        <div class="form-group">
                            <input type="text" placeholder="Search" class="form-control">
                        </div>
                        <button class="btn btn-default" type="submit">Submit</button>
                    </form>
                </div>
            </nav>    
            </div> <!--end data-spy=affix-->

            <!--the LeftSide below just contains basic placeholder text in a <p> tag-->
            @RenderSection("LeftSide", true)

        </div>
        <!--RIGHT SIDE-->
        <div class="col-md-3">

            <!--the RightSide below just contains basic placeholder text in a <p> tag-->
            @RenderSection("RightSide", true)

        </div>
    </div>
</div>

Partially Solved (update) 部分解决(更新)

I was able to solve my main problem that I listed above by using this bit of jQuery 通过使用jQuery,我能够解决上面列出的主要问题

 $(window).resize(function () {
     var w = $("#leftSide").width();
     $("#navWrapper").css("width", w);
 });

But this does not work within an iFrame. 但是,这并不在iFrame中工作。

If anyone has any insights on solving this for use in an iFrame, it'd be greatly appreciated. 如果有人对解决在iFrame中使用此问题有任何见解,将不胜感激。

Have you tried placing your navbar and main content area in one column and then your right sidebar in a column by itself? 您是否尝试过将导航栏和主要内容区域放在一栏中,然后将右侧边栏本身放在一栏中? Then add a bit of CSS to correct for the navbar position and adjust for responsiveness. 然后添加一些CSS来纠正导航栏的位置并调整响应速度。

LINK: http://www.bootply.com/ZJCGxiUD6g 链接: http//www.bootply.com/ZJCGxiUD6g

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

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