html - 在我的页面页脚之前放置一个 div

标签 html css sass

附上页面布局以更好地解释我的要求。 Page layout

  1. 将文本放在页 footer 分之前。
  2. 有时页脚可能不可见(可能需要滚动),在这种情况下,将文本置于可见区域的底部。

我尝试了很多方法来实现这一目标。 解决此问题的任何指示都会有所帮助。

谢谢, 桑托斯

最佳答案

你可以使用 flexbox 来实现这一点

body {
  margin: 0;
  
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.content {
  /* occupy all height */
  flex: 1 0 auto;
  /* nested flex container */
  display: flex;
  flex-direction: column;
}

.bottom-text {
  /* Move to the bottom */
  /* This works because this is flex item */
  margin-top: auto;
}

/* styles just for demo */
body {
  text-align: center;
}

header {
  background-color: tomato;
}

.content {
  background-color: lightsteelblue;
}

.bottom-text {
  background-color: moccasin;
}

footer {
  background-color: lime;
}
<header>Page header</header>
<section class="content">
  Page content
  <div class="bottom-text">Place a text just before footer</div>
</section>
<footer>Page footer</footer>

为了在 footer 不可见时显示 bottom-text,我们将使用 Javascript:

// Checks if element is visible on screen
function checkVisible(element) {
  var rect = element.getBoundingClientRect();
  var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
  return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
}

var footer = document.querySelector("footer");
var bottomText = document.querySelector(".bottom-text");
var bottomTextFixedClassName = "bottom-text--fixed";

// Sets element position as fixed
// when footer is not visible on screen
function setFixedButtonText() {
  if (checkVisible(footer))
    bottomText.classList.remove(bottomTextFixedClassName);
  else
    bottomText.classList.add(bottomTextFixedClassName);
}

window.addEventListener("scroll", setFixedButtonText);

setFixedButtonText();
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.content {
  /* occupy all height by flex-grow: 1 */
  /* Don't shrink using flex-shrink: 0 */
  /* Setting flex-basis to 1500px to emulate long content */
  /* Replace 1500px with auto in production code */
  flex: 1 0 1500px;
  /* nested flex container */
  display: flex;
  flex-direction: column;
}

.bottom-text {
  /* Move to the bottom */
  /* This works because this is flex item */
  margin-top: auto;
}

.bottom-text--fixed {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
}

/* styles just for demo */
body {
  text-align: center;
}

header {
  background-color: tomato;
}

.content {
  background-color: lightsteelblue;
}

.bottom-text {
  background-color: moccasin;
}

footer {
  background-color: lime;
}
<header>Page header</header>
<section class="content">
  Page content
  <div class="bottom-text">Place a text just before footer</div>
</section>
<footer>Page footer</footer>

如果您需要 IE 支持,您可以将 min-height: 100vh; 更改为 height: 100vh;。这是 IE 的 min-height bug for flex with flex-direction: column; 的解决方法。

关于html - 在我的页面页脚之前放置一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955238/

相关文章:

Javascript 正则表达式将链接插入列表

JavaScript Element.value 与 Element.getAttribute ("value")

javascript - 如何在不滚动整个页面的情况下自动滚动内联 div?

javascript - 如何将背景图像放在主体的背景图像上方?

javascript - 不同浏览器对相同字体的不同显示

html - 将 HTML 粘贴到 Word 中同时保留不可见数据

css - 如何修复 "no such file or directory, lstat ' scss/'"?

CSS:无论移动设备的 DPI 如何,都使 div 正好 300 像素高?

html - 将鼠标悬停在 div 上时如何显示复选框

css - 如何将 cssClass 应用于 ionic2 toast