gpt4 book ai didi

html - 在填充父元素剩余空间的 div 上滚动

转载 作者:行者123 更新时间:2023-12-05 04:08:07 25 4
gpt4 key购买 nike

问题

我的网站有固定位置的侧边栏,高度始终为 100%。问题是,在侧边栏中,我有一些静态元素和一个包含动态内容的 div,这些内容应该会填充侧边栏的其余空间。

代码

<div id="sidebar" style="height:100%; background:#73A2; postion:fixed; flex:none; overflow-y:hidden;">
<button>one</button><br/>
<button>two</button><br/>
<button>three</button><br/> //more static content

<div id="scrollableDiv" style="overflow-y:auto; height:100%;">
Really long content
</div>

</div>

结果

我设法得到的几乎是最终效果,因为可滚动的 div 正在填充其余空间,但看起来它需要完整的父高度来设置它的滚动高度。结果是,当我滚动查看动态列表中的最后一个位置时,大约 10% 的滚动溢出到屏幕下方。知道如何在不计算静态元素高度的情况下处理它吗?当 scrollableDiv 的高度设置为例如。到 80%,它没有填充其余空间(大约 10px 左右),但滚动效果很好。

最佳答案

以下是您需要更改的内容以使其适用于此演示:

  • 您需要在sidebarflex-direction:column 上使用display:flex固定宽度 (添加了 top:0, left:0 以及消除任何额外的边距) 以使其灵活(但仅适用于动态长内容 ).
  • overflow-y auto设置动态长内容flex:1;,这样它就会一直灵活且永不溢出

注意:红色背景表示它占用了侧边栏的全部剩余空间。

当有很长的溢出内容时:

#sidebar {
height: 100%;
background: #73A2;
position: fixed;
top:0;
left:0;
flex: none;
overflow-y: hidden;
display:flex;
flex-direction:column;
width:200px;
}

#scrollableDiv {
overflow-y: auto;
background:red;
width:100%;
height: 100%;
flex:1;
}
<div id="sidebar">
<button>one</button><br/>
<button>two</button><br/>
<button>three</button><br/> //more static content

<div id="scrollableDiv">
Really long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentReally long contentv
</div>

</div>

没有溢出内容时:

#sidebar {
height: 100%;
background: #73A2;
position: fixed;
top:0;
left:0;
flex: none;
overflow-y: hidden;
display:flex;
flex-direction:column;
width:200px;
}

#scrollableDiv {
overflow-y: auto;
background:red;
width:100%;
height: 100%;
flex:1;
}
<div id="sidebar">
<button>one</button><br/>
<button>two</button><br/>
<button>three</button><br/> //more static content

<div id="scrollableDiv">
contentReally long contentReally long contentReally long contentReally long content
</div>

</div>

关于html - 在填充父元素剩余空间的 div 上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47968386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com