简体   繁体   English

如何在不创建滚轮的情况下让 div 填充屏幕的剩余高度?

[英]How do I get a div to fill the remaining height of the screen, without creating a scrollwheel?

I am in the process of starting a WebGL project and have created a functional NavBar at the top using Bootstrap 5. I am trying to fill the remaining space with a canvas where I will be rendering my work.我正在启动一个 WebGL 项目,并使用 Bootstrap 5 在顶部创建了一个功能性导航栏。我正在尝试用画布填充剩余空间,我将在其中渲染我的作品。 However using height: 100%;但是使用height: 100%; , to my guess seems to be trying height to not be including the NavBar and is going too far below the screen creating a scrolling wheel. ,我的猜测似乎是尝试高度不包括 NavBar 并且在屏幕下方太远创建一个滚轮。

I am creating a working example shown here https://jsfiddle.net/cL40tzg9/6/ that will show what I mean.我正在创建一个此处显示的工作示例https://jsfiddle.net/cL40tzg9/6/ ,它将显示我的意思。 I am simply trying to create a NavBar and will the remaining space with my canvas, preventing any unwanted scrolling.我只是想创建一个 NavBar 并将剩余空间与我的画布一起使用,以防止任何不需要的滚动。 What have I done wrong here?我在这里做错了什么? Thanks谢谢

The JavaScript is (minified a little), JavaScript 是(缩小一点),

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand h1" style="cursor: default"> example </a>
  </div>
</nav>

<canvas id="scene_container"> </canvas>

with the CSS being,使用 CSS,

html, body {
    height:100%;
    width: 100%;
    margin: 0;
    padding: 0;
    background-color: transparent;
}

#scene_container {
    width: 100%;
    height: 100%;
    background: transparent;
    z-index: 0; 
}

A simple Flex approach to set the canvas to the full height minus the nav height might look a little like this.将画布设置为全高度减去导航高度的简单 Flex 方法可能看起来有点像这样。 The following uses a css var --nav-height for convenience and background colours applied to illustrate the effect.为方便起见,以下使用 css var --nav-height并应用背景颜色来说明效果。

 :root{ --nav-height:3rem; } html, body { height:100vh; max-height:100vh; width: 100%; margin: 0; padding: 0; background-color: transparent; } body,body *{ box-sizing:border-box; margin:0; } body{ display:flex; flex-direction:column; } nav{ height:var(--nav-height); min-height:var(--nav-height); max-height:var(--nav-height); width:100%; flex:1; background:yellow; } canvas{ width:100%; height:calc( 100vh - var(--nav-height) ); flex:100; background:pink; }
 <nav class='navbar navbar-expand-lg navbar-light bg-light'> <div class='container-fluid'> <a class='navbar-brand h1' style='cursor: default'> example </a> </div> </nav> <canvas id='scene_container'></canvas>

you either need to use flex or deduct the height of nav from canvas , if nav has static height.你要么需要使用flex或扣除的高度navcanvas ,如果nav有静态的高度。

#scene_container {
    width: 100%;
    height: calc(100% - 64px);
    background: transparent;
    z-index: 0; 
}

64px is your current nav height. 64px是您当前的导航高度。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM