html – 拉伸块元素以填充其父元素的100%垂直滚动高度
我有一个垂直滚动的布局.可滚动div中的一个子元素绝对定位为具有较大的顶部值,从而在父级上引入垂直滚动条.
这是HTML标记: <div id="root" style="height: 250px; position: relative;"> <div class="stretch"> <div id="container" class="container"> <div id="pillar1" style="left: 0.0%; width:33.25%;" class="pillar" ></div> <div id="pillar2" style="left: 33.05%; width:33.25%;" class="pillar" ></div> <div id="pillar3" style="left: 66.05%; width:33.25%;" class="pillar" ></div> <div id="fixed-and-not-movable" style="background: blue; width: 25px; height: 25px; top:350px; left: 150px; position: absolute;"> </div> </div> 和CSS: .stretch { bottom: 0; left: 0; right: 0; top: 0; position: absolute; height: auto; width: auto; } .container { border: 2px solid; bottom: 0; left: 0; right: 0; top: 0; overflow-x: hidden; overflow-y: scroll; position: absolute; } .pillar { border: 1px dotted red; bottom: 0; height: 100%; position: absolute; top: 0; } 我希望柱子div捕获父“容器”的整个滚动高度.现在他们的身高是父母的客户身高(不是滚动身高).因此,当您向下滚动时,您会注意到柱子没有填满溢出内的所有可用高度:滚动. 有人可以建议更改CSS类(.container和/或.pillar)以使其工作. 这是一个链接到js小提琴显示相同的问题: 谢谢! 解决方法经过大量的实验和拔毛,我终于发现没有完美的CSS解决方案来解决这个问题.如果有人能证明我错了,我会很高兴.我理解的问题是,如果未明确定义父级高度,则无法通过纯交叉浏览器兼容的CSS将子元素垂直拉伸100%以填充其父级scrollHeight. 因此,通过上述结论,我通过在滚动容器下放置一个显式大小的div并在支柱上设置显式的最小高度来解决这个问题.我可以通过JavaScript计算这个新的中间div的高度. 这是修改后的HTML标记(只有fixedMinHeight div是新的) <div id="root" style="height: 250px; position: relative;"> <div class="stretch"> <div id="container" class="container"> <div id="fixedMinHeight" class="stretchFixed"> <div id="pillar1" style="left: 0.0%; width:33.25%;" class="pillar" ></div> <div id="pillar2" style="left: 33.05%; width:33.25%;" class="pillar" ></div> <div id="pillar3" style="left: 66.05%; width:33.25%;" class="pillar" ></div> <div id="fixed-and-not-movable" style="background: blue; width: 25px; height: 25px; top:350px; left: 150px; position: absolute;"> </div> </div> </div> 和CSS(.stretchFixed是从前面添加的) .stretch { bottom: 0; left: 0; right: 0; top: 0; position: absolute; height: auto; width: auto; } .container { border: 2px solid; bottom: 0; left: 0; right: 0; top: 0; overflow-x: hidden; overflow-y: scroll; position: absolute; } .pillar { border: 1px dotted red; bottom: 0; height: 100%; position: absolute; top: 0; } .stretchFixed { min-height: 375px; position: relative; height: 100%; } 这是与变化的小提琴链接:https://jsfiddle.net/AshwinPrabhuB/2o5whkmq/10/ 或者,可以在每个单独的柱上应用相同的方案,从而不需要DOM插入. 我希望被证明是错的,但暂时我可以忍受这种解决方法. (编辑:湘西站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |