【问题标题】:jQuery to place a div at a particular position after calculating window heightjQuery在计算窗口高度后将div放置在特定位置
【发布时间】:2015-04-13 12:40:23
【问题描述】:

我想放置一个基于使用 jQuery 计算窗口高度的 div。 这是我的代码 sn-p:

var winHeight = document.body.scrollHeight;

现在我得到了窗口的高度。 我想根据高度在窗口底部放置一个 div。 这是因为高度不同,但与高度无关,这个DIV应该通过给这个DIV一些样式来放置在底部。

注意:我通过 jQuery 动态添加 div。所以我需要计算页面的高度并显示出来。因为页面的内容/高度会有所不同。这就是为什么我需要通过 jQuery 添加基于窗口高度的 css 样式

请帮忙。

谢谢,

【问题讨论】:

  • $('div').css('top', $(window).height() - $('div').height()),或更简单的bottom : 0px
  • 您可以单独在 CSS 中执行此操作,在 div 上使用position: fixed; bottom: 0,然后在与固定 div 高度匹配的容器上使用margin-bottom 以停止任何重叠。

标签: javascript jquery html css window


【解决方案1】:

您不需要 JavaScript。我建议使用 CSS position: fixedbottom: 0:

#bottom {
  position: fixed;
  bottom: 0;
  background: red;
}
<div id="bottom">I'm always at the bottom</div>

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 2015-06-26
    • 2015-02-12
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多