javascript - jQuery 将页脚贴在页面底部

标签 javascript jquery css

我花了很长时间尝试使用 CSS 将页脚固定在页面底部,但几乎放弃了。

如果视口(viewport)的高度小于 HTML 文档的高度,我想要的是不为页脚分配额外的 CSS 属性。

如果文档高度小于窗口高度,我希望将以下 CSS 分配给 div#thefooter:

position: fixed;
bottom: 0px;
margin-left: -5px;

这是我的 JS 代码。它什么都不做,控制台日志什么也不显示。

$(document).ready(function(){

  $(window).bind("load", function(){ putFooter(); });
  $(window).resize(function(){ putFooter(); });

  function putFooter(){
    var wh = $(window).height();
    var dh = $(document).height();
    if(dh < wh) {
      $("#thefooter").css({"position": "fixed", "bottom": "0px", "margin-left": "-5px"});
      }
    }

});

编辑:这是我的 HTML 的样子:

<div id="allexceptfooter">
  <div id="themenu">...</div>
  <div id="actualcontent">...</div>
</div>
<div id="thefooter">...</div>

如果您想查看全部内容,我的网站是 duncannz .com

最佳答案

检查一下,根本不需要 javascript...

http://www.cssstickyfooter.com

关于javascript - jQuery 将页脚贴在页面底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10964662/

相关文章:

javascript - 如果多边形已存在则禁用绘制

javascript - 我的网站卡住 - 可能是 JavaScript,但我无法确定原因

html - d-flex div 中的 img 导致图像在 IE 11 的 Bootstrap 4 中溢出

html - inline-block不排在同一行

html - 使 ul 列表项的行为与其附近的表的行为相同

javascript - 多个语义 UI 弹出窗口,其中目标元素在每个实例的属性中定义

javascript - 什么是 $cacheFactory?

javascript - Shiny 的 tabPanel 和 Google Analytics

jquery - .is() - 使用 AND 代替 OR 条件

javascript - jQuery 验证在公共(public)文件中不起作用