jquery - 将 Div 定位在窗口高度以下

标签 jquery html css

让我们看下面的例子

<div>Main Content<br><br>Main Content which fills windows height</div>
<div id="belowcontent">Content to be show below the windows height</div>

我试过类似下面的东西

$("#belowcontent").css("margin-top", $(window).height());

但这里的问题是 div 使用的是之前 DIV 的高度,而不是浏览器顶部。我也尝试了绝对位置。仍然无法正常工作。

P.s: 如果是纯CSS方法,那就是优势

最佳答案

绝对定位

您可以使用 absolute 定位和 top 声明。在这种情况下,我将 top 设置为 100vh 以将其置于屏幕高度以下。

html,
body {
  margin: 0;
  padding: 0;
  background: lightgray;
}
.main {
  background: cornflowerblue;
}
#belowcontent {
  position: absolute;
  top: 100vh;
  background: tomato;
}
<div class="main">Main Content
  <br>
  <br>Main Content which fills windows height</div>
<div id="belowcontent">Content to be show below the windows height</div>


相对定位

如果您不想使用定位,您可以在 main div 上使用(代替)一个 min-height 属性:

html,
body {
  margin: 0;
  padding: 0;
}
.main {
  min-height: 100vh;
  background: cornflowerblue;
}
#belowcontent {
  background: tomato;
}
<div class="main">Main Content
  <br>
  <br>Main Content which fills windows height</div>
<div id="belowcontent">Content to be show below the windows height</div>

关于jquery - 将 Div 定位在窗口高度以下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761258/

相关文章:

php - 从表内的文本框中获取值并使用 JQuery 将其存储为变量

jquery - 如何强制 navbar-fixed-top 在一定高度后显示

html - 水平居中 <section>

javascript - 在多个 div 之间切换可见性

Javascript:使用 Jquery 在父输入中键入时更新标签内容

javascript - 外部JS : How to enforce the MaxLength to false at run time?

css - 人造列 - IE 与 Chrome - 不同的不透明度?

JavaScript 文件路径问题/错误

javascript - 如何使用 jquery 获取以前的标签 (td) 子属性值?

javascript - 选择或提取单个基于 JavaScript 的动画代码的最有效方法?