maintain 100% height with vertical window scroll

那年仲夏 提交于 2021-02-08 07:23:39

问题


I Have a basic bootstrap grid that I need to vertically fill the window regardless of scrolling. Currently, if the window scrolls all styles end sharply at point of scroll.

I need both divs to be the length of the longest of the two divs, and to scroll with each other.

Here is a fiddle of the problem: https://jsfiddle.net/frxpngcn/2/ Thank you.

<div class="row">
<div id="nav" class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
    <ul>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
        <li>Link</li>
    </ul>
</div>
<div id="main" class="col-xs-10 col-sm-10 col-md-10 col-lg-10">
    <h1>Header</h1>
    <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>

html, body, .row {
    height: 100%;
}

#nav {
    height: 100%;
    background-color: green;
}

#main {
    height: 100%;
    background-color: yellow;
}

回答1:


CSS Overflow https://jsfiddle.net/8a74be3L/

This options makes the main div scroll not the page

#main {
    height: 100%;
    background-color: yellow;
    overflow-y:scroll;
}

CSS Padding https://jsfiddle.net/8a74be3L/3/

A way to do it in pure css is to add a stupid amount of padding to the bottom and then hide the overflow in the container class.

.main-container {
     overflow: hidden;
 }

#nav {
     float: left;
     background-color: green;
     padding-bottom: 500em;
     margin-bottom: -500em;
 }

 #main {
     float: left;
     background-color: yellow;
     padding-bottom: 500em;
     margin-bottom: -500em;
 }

JS Option https://jsfiddle.net/8a74be3L/2/

This option uses js to get the height of the main div and set it as the height of the nav div.

function resize() {
    var h = document.getElementById('main').offsetHeight;
    document.getElementById("nav").style.height = h + "px";
}
resize();
window.onresize = function () {
    resize();
};


来源:https://stackoverflow.com/questions/32074698/maintain-100-height-with-vertical-window-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!