首页 > 解决方案 > 如何从 windowheight 获取滚动 div 的底部偏移量

问题描述

我只想偏移红色框的底部空间,当我滚动正文滚动时,红色框将某些部分隐藏到正文滚动中,我将单击一个按钮以获取红色框的底部偏移量参见图片以获取参考

//want offset bottom of box  after some scroll of body scroll
.divHasScroll {
  height: 200px;
  width: 350px;
  background: red;
  overflow: auto
}
<div class="divHasScroll">
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
  <p> text in box</p>
</div>

<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>
<p> text in window scrolling section</p>

标签: jqueryhtmlcss

解决方案


如果您的容器是主体,您可以通过此计算获得偏移量

alert($('body').height() - $('.divHasScroll').height());

您需要容器的高度 - 可滚动 div 的高度。

偏移属性仅适用于顶部或左侧,在这种情况下它不会为您服务。

希望这可以帮助。


推荐阅读