【问题标题】:How to make sidenav increase/decrease in height, to always fill the entire webpage如何使sidenav增加/减少高度,以始终填满整个网页
【发布时间】:2019-01-19 21:56:01
【问题描述】:

我一直在尝试制作一个具有标题(顶部)、sidenav(左侧)和网站主要内容的网站。现在这里是一个布局示例

https://jsfiddle.net/qdza4bxc/1/

现在,在学习了一点 JavaScript 之后,我做了一个函数,可以在页面滚动时增加/减少 sidenav 的顶部填充,下面是示例:

https://jsfiddle.net/4vtuq6m8/

但这会在侧导航内部的顶部留下一个很大的空隙。所以我想知道 w3school 是如何做到这一点的,当您向下滚动他们的页面时:https://www.w3schools.com/html/default.asp 您可以看到当您向下滚动他们的网站时,侧边栏的高度和内容始终保持在完美的位置。

如果有人对此有解决方案,我会很高兴!

【问题讨论】:

  • w3school 上的侧边菜单使用位置:固定顶部:0 左:0 高度:100vh 溢出:滚动......或一些小的变化......没看,但这只是我的猜测跨度>

标签: javascript html height


【解决方案1】:

var header = document.querySelector('header');
var headerHeight = header.offsetHeight;
var sidebar = document.querySelector('#sidebar');

document.addEventListener('scroll', function(e) {
  // Check how many pixels have been scrolled
  var scrollTop = document.documentElement.scrollTop;
  // offset should never be higher than the header
  scrollTop = scrollTop > headerHeight ? headerHeight : scrollTop;
  // Calculate the final offset to apply to the height
  var offsetTop = headerHeight - scrollTop;

  sidebar.style.height = 'calc(100% - ' + offsetTop + 'px)';
});
html,
body {
  height: 100%;
  margin: 0;
}

header {
  width: auto;
  height: 100px;
  background-color: indianred;
}

#sidebar {
  width: 20%;
  height: calc(100% - 100px);
  position: fixed;
  bottom: 0;
  background-color: gray;
  float: left;
  overflow-y: auto;
}

#sidebar h2 {
  margin: 0;
}

section {
  width: 80%;
  float: right;
  background-color: antiquewhite;
}
<header></header>
<div id="sidebar">
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Hi there</h2>
  <h2>Soon done</h2>
  <h2>Done!</h2>
</div>
<section>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h1>Main Content</h1>
  <p>This is just some text that i put inn to make this look some what of a website</p>
  <h2>Next Section</h2>
  <p>This is just some text that i put inn to make this look some what of a website</p>
</section>

【讨论】:

  • 嗨!这解决了我的问题,但我永远不会自己解决这个问题,尤其是最后一个“js”声明。这是您自己编写代码的方式,还是您可能知道的不太先进的方式?
  • 嘿@SimenPedersen,我试图根据你的主要实现找到一个简单的解决方案,我认为这并不先进,只是有点逻辑,但如果你逐行遵循,那就是很简单,如果有什么不明白的请告诉我;)
  • 好的,感谢您的帮助和尝试更简单的解决方案!我想我唯一不明白的部分是你可以在 js 字符串中使用 css 的 calc() 扩展:)
  • 这正是您在 CSS 文件 height: calc(100% - 100px) 中使用的内容,但不是硬编码值 100px,而是在滚动事件中计算它以检查标题的高度是多少在视口中。
猜你喜欢
  • 2014-01-07
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2017-11-01
相关资源
最近更新 更多