简体   繁体   English

如何使用CSS使固定元素停留在父对象的可见边界内?

[英]How to make fixed element stay within parent's visible borders with CSS?

This is my layout, yellow part is fixed. 这是我的布局,黄色部分固定。 I made a simple script to make the sidebar shrink/grow so that it never goes over the green footer, is it possible to achieve the same with CSS only? 我编写了一个简单的脚本来使边栏缩小/增长,以使其永远不会超出绿色页脚,仅使用CSS就能实现相同的功能吗?

See it in action here: http://jsfiddle.net/RWxGX/203/ 在此处查看其运行情况: http : //jsfiddle.net/RWxGX/203/

在此处输入图片说明

I made a simple jquery script so that the fixed yellow sidebar shrinks when the green part goes up when scrolling the window 我制作了一个简单的jquery脚本,以便在滚动窗口时,当绿色部分上升时,固定的黄色侧边栏会缩小

HTML, CSS, JS: HTML,CSS,JS:

<div id="content">
  <div id="sidebar">
    <div id="sidebar-container">
      <div id="sidebar-wrapper">
        <div id="sidebar-content">
          height: 300px
        </div>
      </div>
    </div>
  </div>
  <div id="article">
    Scroll me to see the sidebar shrink
  </div>
</div>
<div id="footer">

</div>

body {
  padding: 0;
  margin: 0;
  min-height: 100%;
}
#content {
  display: flex;
  justify-content: stretch;
}

#sidebar {
  height: inherit;
  width: 100px;
  flex: 0 0 auto;
  background-color: red;
}

#article {
  background-color: orange;
  flex-grow: 1;
  font-size: 55px;
}

#sidebar-container {
  position: fixed;
  max-height: 100%;
  height: inherit;
  width: inherit;
  background-color: red;
  display: flex;
  flex-direction: column;
}

#sidebar-wrapper {
  background-color: yellow;
  overflow-y: scroll;
}

#sidebar-content {
  background-color: rgba(255, 0, 0, 0.1);
  height: 20000px;
}

#mainbar {
  background-color: blue;
}

#content {
  height: 300px;
  width: 500px;
  background-color: rgba(255, 255, 255, 0.75);
}
#footer {
  height: 2000px;
  width: 500px;
  background-color: green;
}

$(document).ready(function() {
    var sidebar_container = $('#sidebar-container');
  var sidebar_content = $('#sidebar-content');
  var footer = $('#footer');
  $(window).scroll(function () {
    sidebar_container_height = 
        Math.max(
        Math.min(
            footer.offset().top - $(window).scrollTop(),
          $(window).height()),
      0);
    sidebar_container.css('height', sidebar_container_height + 'px');
    sidebar_content.text('height: ' + sidebar_container_height + 'px');
  })
})

I think that in some cases it can be solved using position:absolute;z-index:999 on #footer . 我认为在某些情况下,可以在#footer上使用position:absolute;z-index:999#footer But this causes footer to cover sidebar, not to resize sidebar. 但这会导致页脚覆盖侧栏,而不是调整侧栏的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用具有“position:fixed”元素的代码段中的代码? 我无法让它留在我的内心 <div> 身高(父母) - how can I use a code from a snippet which have a “position:fixed” element ? I can't make it stay inside my <div>'s height (parent) 如何仅在滚动时使元素固定但相对于其父元素? - How to make an element fixed but relative to it's parent on scrolling only? 如何使固定元素相对于当前顶部滚动位置停留? - How to Make a Fixed Element Stay Relative to the Current Top Scroll Position? 如何使CSS元素超出包含div的所有边界? - How to make a CSS element extend beyond all borders of containing div? CSS位置:固定<script> tag within does not stay fixed - css position:fixed <script> tag within does not stay fixed 如果固定元素超出父元素的高度,该如何隐藏? - How to hide a fixed element if outside of it's parent element's height? 如何检测元素是否在跨域父级的iframe中可见 - How to detect if an element is visible within an iframe that is on a cross domain parent jQuery可拖动+可调整大小导致问题,元素不在其父元素之内 - Jquery draggable + resizable causing issues, element doesn't stay within it's parent 在href之后如何使导航栏保持可见? - How to make navbar stay visible, after href? 如何使工具提示在mousenter上保持可见? - How to make a tooltip stay visible on mousenter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM