javascript - 页脚的动态绝对位置,缺少像素 - 无限滚动

标签 javascript jquery html css css-position

我需要有一个与其父级相关的页脚,我需要将其固定在窗口底部。

我进行了计算以获得正确的 top 值,但我缺少 8 个像素 - 这会导致滚动无限滚动。

我在计算中遗漏了什么?

$(function () {
  changeFooter();
});

$(window).resize(function () {
  changeFooter();
});
$(window).scroll(function () {
  changeFooter();
});

function changeFooter() {
  var footer = $("#footer");
  footer.css({ top: getFooterTop(footer) + 'px' });
}

function getFooterTop(footer) {
  return window.innerHeight + $(window).scrollTop() - footer.height();
}

function getFooterTopFixed(footer) {
  return window.innerHeight + $(window).scrollTop() - footer.height() - 8;
}
#wrap {
  position:absolute;
  left:0px;
  width: 100%;
  margin-bottom: 50px;
}

#footer {
  position: absolute;
  width: 100%;
  height: 50px;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap">
  <p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p><p>Content</p>
  <div id="footer"></div>
</div>

https://jsfiddle.net/rs4f1jt0/2/

你可以在链接中看到 -8 像素修复了定位,我只是想不通那 8 个像素来自哪里来修复计算。

谢谢。

最佳答案

为什么不尝试使用固定在页脚上的位置?像这样:

#wrap {
 position:relative;
 left:0px;
 width: 100%;
 margin-bottom: 50px;
}

#footer {
 position: fixed;
 bottom:0;
 width: 100%;
 height: 50px;
 background-color: green;
}

关于javascript - 页脚的动态绝对位置,缺少像素 - 无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34973825/

相关文章:

javascript - 如何在 Angular 8之间共享数据

javascript - 对从 jQuery ui 自动完成选项列表中选择的选项执行函数

asp.net - PartialView $ ('#webgrid' ).html(data) 导致 webgrid col 单击仅显示网格且不带 css?

javascript - 有没有办法在月 View 中向 Kendo Scheduler 添加 'X more events' 样式的消息?

javascript - 未捕获的类型错误 : Cannot set property 'innerHTML' of null

javascript - 当我使用子路径时,快速加载 Controller 无限次并且浏览器窗口崩溃

javascript - 我怎样才能对内部函数中的变量进行任何更改以影响主变量值

javascript - 自动查找用户位置

ruby-on-rails - 将基于时间的字符串转换为 ISO 格式

html - TTY 电话号码是否应该作为普通号码进行超链接?