【问题标题】:CSS: Height of an fixed Element to the bottom of the viewportCSS:固定元素到视口底部的高度
【发布时间】:2022-01-03 07:00:58
【问题描述】:

我的页面上有一个 iframe (class:side_bar),按位置定位:固定在我的文本流中的某个点。它的顶部位置可能在 100-200 像素之间变化。无论其内容如何,​​此元素的高度都应达到视口的底部。
我尝试了 height:100vh 和 height:100%,但是它是整个窗口的高度,并且元素下端被浏览器框架切割。
有什么方法可以获取元素的初始顶部位置,以便我可以使用 heigh:calc(100%-top-position) 或一些简单的选项将该元素拉伸到浏览器的底部(而不是底部页面,因为它可能更长)?
非常感谢(抱歉我的英语不好;))

编辑:我做了一个小例子 - 不完美,但你可以明白我的意思......

.main
    {
    width:100%;
    }
.header
    {
    width:100%;
    text-align:right;
    border-style: solid;
    border-width:1px
    }
.side_bar
    {
    position:fixed;
    width:150px;
    height:100%;
    border-style: solid;
    border-width:1px
    display:inline-block;
    }
.content
    {
    margin-left:150px;
    padding:10px;
    border-style: solid;
    border-width:1px
    }
<div class="main">
    <div class="header">
        <b>Header:</b><br>Height may vary, around 100px-200px, depending on content.
        </div> 
    <iframe class="side_bar" srcdoc="
        <b>Side bar:</b>
        <p>Content may vary, sometimes scrolling, sometimes not</p>
        <p>This iframe should end at the bottom of the viewport, and begin below Header - regardless of content</p>
        <p>By scrolling the page if needed, this iframe must stay in place, therefor position:fixed</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        "> </iframe>
    <div class="content">
        <b>Main content:</b>
        <p>Content may vary, sometimes scrolling, sometimes not</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
        </div>
    </div> 

【问题讨论】:

标签: css position height fixed bottom


【解决方案1】:

您的问题非常不清楚,希望这是您想要的

document.querySelector(".content").innerHTML = "<p>content</p>".repeat(60);


const header = document.querySelector(".header");
const sidebar = document.querySelector(".side_bar");
sidebar.style.top = header.offsetHeight + "px";

header.onchange = () => {
  sidebar.style.top = header.offsetHeight + "px";
};
onscroll = () => {
  const diff = header.offsetHeight - window.pageYOffset;
  if (diff < 0) {
    sidebar.style.top = 0;
  } else {
    sidebar.style.top = diff + "px";
  }
};
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.main {
  width: 100%;
}

.header {
  width: 100%;
  text-align: right;
  border: 0.3px solid;
}

section {
  display: flex;
}

.side_bar {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 20vw;
  height: 100%;
  border: 0.3px solid;
}

.content {
  margin-left: 20vw;
  width: 80vw;
  padding: 1vh;
  border: 0.3px solid;
}
<div class="main">
  <div class="header">
    <p>header</p>
  </div>

  <section>
    <iframe class="side_bar" srcdoc="
          <b>Side bar:</b>
          <p>Content may vary, sometimes scrolling, sometimes not</p>
          <p>This DIV should end at the bottom of the viewport, and begin below Header - regardless of content</p>
          <p>By scrolling the page if needed, this DIV must stay in place, therefor position:fixed</p>
          <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
          <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
          <p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p><p>content</p>
          ">
        </iframe>

    <div class="content">
      <b>Main content:</b>
      <p>Content may vary, sometimes scrolling, sometimes not</p>
    </div>
  </section>

</div>

【讨论】:

  • 对不起,我的问题有错误,我更正了它,使它更清楚一点....header的高度不能设置,所以一个具体的值,它取决于.标题,正在发生变化。唯一的问题是 side_bar 的高度 - 如果设置为 height:100%,则下端 ist cut 与您的示例一样。解决方法是 height:calc(100%-30px) 和 top:30px,但是当 .header 变高时,它会重叠...
  • @Matthias 现在怎么样?
  • 否 :( 现在 - 如果您向 .content 添加更多内容,您必须滚动 .content 才能查看 .side_bar 中的所有内容。.side_bar 应该保持原位,即使主页已滚动 - 两个区域应完全独立滚动...
  • @Matthias 现在怎么样
猜你喜欢
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多