我已经使用twitter引导框架很长一段时间了,他们最近更新到版本3!

我有麻烦让粘性页脚粘到底部,我已经使用了twitter bootstrap网站提供的启动器模板,但仍然没有运气,有什么想法吗?


当前回答

基于jk -fdrv的回答,我添加了一个.on('resize',函数(),以确保它在每种设备和每种分辨率上都能工作:

$(window).on('resize', function() {
    if ($(document).height() <= $(window).height()) {
        $('footer').addClass("navbar-fixed-bottom");
    } else {
        $('footer').removeClass("navbar-fixed-bottom");
    }
});

其他回答

如果你想在类中使用自举构建页脚。你还应该写一些javascript:

$(document).ready(function(){
  $.fn.resize_footer();

  $(window).resize(function() {
    $.fn.resize_footer();
  });
 });

(function($) {

  $.fn.resize_footer = function(){
    $('body > .container-fluid').css('padding-bottom', $('body > footer').height());
  };
 });

它将通过固定的页脚防止内容重叠,并且当用户更改窗口/屏幕大小时,它将调整填充底部。

在上面的脚本中,我假设footer直接放置在body标签中,就像这样:

<body>
  ... content of your page ...
  <div class="navbar navbar-default navbar-fixed-bottom">
    <div class="container">
      <div class="muted pull-right">
        Something useful
      </div>
      ... some other footer content ...
    </div>
  </div>
</body>

这绝对不是最好的解决方案(因为JS可以避免),但它没有任何重叠的问题,它很容易实现和响应性(高度不是硬编码在CSS中)。

除了刚才添加的CSS之外,还需要在关闭wrap div之前添加push div

HTML的基本结构是

<div id="wrap"> 
    page content here 
    <div id="push"></div>
</div> <!-- end wrap -->

<div id="footer">
    footer content here
</div> <!-- end footer -->

实时视图 编辑视图

当前版本的bootstrap似乎不再适用于这个函数了。如果你下载了他们的sticky-footer-navbar的例子,并在主体区域放置了大量的内容,footer将被向下推过视口的底部。它一点也不粘。

我想要一个灵活的粘性页脚,所以我才来这里。最重要的答案让我找到了正确的方向。

当前(10月16日2日)Bootstrap 3 css Sticky footer(固定大小)看起来像这样:

html {
  position: relative;
  min-height: 100%;
}
body {
  /* Margin bottom by footer height */
  margin-bottom: 60px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  /* Set the fixed height of the footer here */
  height: 60px;
  background-color: #f5f5f5;
}

只要页脚的大小是固定的,body margin-bottom就会产生一个推力,让页脚可以坐进去。在本例中,两者都设置为60px。但如果页脚不是固定的,高度超过60px,它将覆盖你的页面内容。

使灵活:删除css正文边距和页脚高度。然后添加JavaScript来获取页脚高度,并设置body的marginBottom。这是通过setfooter()函数完成的。接下来添加事件监听器,用于页面第一次加载和调整大小时运行setfooter。注意:如果你的页脚有一个手风琴或其他触发大小变化的东西,而没有调整窗口的大小,你必须再次调用setfooter()函数。

运行代码片段,然后全屏演示。

function setfooter(){ var ht = document.getElementById("footer").scrollHeight; document.body.style.marginBottom = ht + "px"; } window.addEventListener('resize', function(){ setfooter(); }, true); window.addEventListener('load', function(){ setfooter(); }, true); html { position: relative; min-height: 100%; } .footer { position: absolute; bottom: 0; width: 100%; /* additional style for effect only */ text-align: center; background-color: #333; color: #fff; } body{ /* additional style for effect only not needed in bootstrap*/ padding:0; margin: 0; } <div> Page content <br> <br> line 3 <br> <br> line 5 <br> <br> line 7 </div> <footer id="footer" class="footer"> <div class="container"> <p class="text-muted">Footer with a long text, so that resizing, to a smaller screen size, will cause the footer to grow taller. But the footer will not overflow onto the main page.</p> </div> </footer>

使用flexbox是我从CSS-tricks的人那里找到的最简单的方法。这是真正的sticky-footer,它在内容< 100%页面和> 100%页面时工作:

<body>
  <div class="content">
  content
</div>
<footer></footer>
</body>

和CSS:

html {
  height: 100%;
}
body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1;
}

请注意,这是bootstrap-不可知论的,所以它可以使用bootstrap,也可以不使用bootstrap。