【问题标题】:Bootstrap fluid-layout and full height divBootstrap 流体布局和全高 div
【发布时间】:2013-03-25 20:11:52
【问题描述】:

我目前正在使用 Twitter 的 Bootstrap,并试图得到一些具体的东西,但无法让它工作......

我想要一个带有导航栏、侧边栏、内容和页脚的流畅布局。 我还想要一个全高的内容内的 div。

HTML

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container-fluid">
            <a class="brand" href="#">Navbar</a>
        </div>
    </div>
</div>

<div class="container-fluid">
    <div class="row-fluid">
        <div class="span3">
            <div class="well sidebar-nav">
                <ul class="nav nav-list">
                    <li class="nav-header">Sidebar</li>
                </ul>
            </div><!--/.well -->
        </div><!--/span-->

        <div class="span9">
            <div class="hero-unit">
                <h1>Hero Unit</h1>
            </div>

            <div class="row-fluid">
                <div class="span12">
                    <div id="fullHeight" style="background-color: red">
                        <p>Full height here</p>
                    </div>
                </div><!--/span-->
            </div><!--/row-->
        </div><!--/span-->
    </div><!--/row-->

    <hr>

    <footer>
        <p>&copy; Company 2013</p>
    </footer>

</div><!--/.fluid-container-->

CSS

body {
        padding-top: 60px;
        padding-bottom: 40px;
    }
    .sidebar-nav {
        padding: 9px 0;
    }

    @media (max-width: 980px) {
        /* Enable use of floated navbar text */
        .navbar-text.pull-right {
            float: none;
            padding-left: 5px;
            padding-right: 5px;
        }
    }

这是 jsfiddle 向您展示我拥有/想要的东西: http://jsfiddle.net/2nuvP/

有人可以帮我解决这个问题吗?

提示:我不确定这是否仅适用于 CSS,我接受 JS/jQuery 解决方案;) 此外,如果您的解决方案需要一些 HTML 修改,这也没什么大不了的......

【问题讨论】:

  • 没有绝对定位您的页眉和页脚就无法完成。 stackoverflow.com/questions/14986873/…
  • 我刚刚尝试了您帖子中描述的解决方案,但无法按我的意愿工作:英雄单位或其他任何东西,即在我的“全高 div”现在被屏蔽之前...或者也许我错过了 CSS 的一些东西?

标签: jquery css twitter-bootstrap fluid-layout


【解决方案1】:

这里主要使用 jQuery 来解决您的问题。

http://jsfiddle.net/uyEuN/4/

JavaScript

function resolveFullHeight() {
    $("#fullHeight").css("height", "auto");

    var h_window = $(window).height(),
        h_document = $(document).height(),
        fullHeight_top = $("#fullHeight").position().top,
        est_footerHeight = 112;

    var h_fullHeight = (-1 * (est_footerHeight + (fullHeight_top - h_document)));

    $("#fullHeight").height(h_fullHeight);
}

resolveFullHeight();

$(window).resize(function () {
    resolveFullHeight();
});

除了在导航栏下方添加这个 div 之外,我大部分时间都将 HTML 放在一边。

<div class="spacer-fluid-60"></div>

CSS 包含设置.spacer-fluid-60 div 高度的新规则, 我还删除了body 元素的padding-top 规则。调查 jsfiddle 以获得完整的详细信息。

在 HTML 中,我添加了许多重复的填充段落,并将其中的大部分注释掉了。根据需要取消注释它们,以调整 #fullHeight 元素中内容高度的变化,看看它是否仍然按照您的预期运行。到目前为止,我所做的最小测试表明这将起作用。

注意:添加一些throttling 以减少滚动时调用函数的次数。

【讨论】:

  • 这个解决方案实际上非常好用,而且完全符合我的要求!刚刚添加了一个“on DOM change event”以在某些情况下自动调整大小,这很好。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
相关资源
最近更新 更多