简体   繁体   English

div 设置为高度 100%,使用 scroll-y: hidden,但是我如何让子 div 具有可滚动性?

[英]Div is set to height 100% with scroll-y: hidden, BUT how do i get a child div to have scrollable?

Since this is purely HTML/CSS issue I did not tag angular as I reproduced it purely in HTML as a fiddle.由于这纯粹是 HTML/CSS 问题,我没有标记 angular,因为我纯粹在 HTML 中复制了它作为小提琴。

I have a div with a height:100% and overflow-y: hidden so the div maintains the viewport.我有一个height:100%overflow-y: hidden的 div,因此 div 保持视口。

The issue though I noticed is that a div in there is populated by ngFor and scales off the page but it won't let me set overflow-y: scroll thinking it works.虽然我注意到的问题是其中的一个 div 由 ngFor 填充并缩小页面但它不会让我设置overflow-y: scroll认为它有效。 It didn't though, so I'm a bit confused.不过没有,所以我有点困惑。

.banner { height: 100px; }
.content { height: calc(100% - 150px); }
.footer { height: 50px; }

<div class="banner"></div>
<div class="content">
  <router-outlet></router-outlet>
</div>
<div class="footer"></div>

And then in the child component:然后在子组件中:

.content { overflow-y: hidden; }
.table { overflow-y: scroll; }

<div class="content">
  <div class="title">{{title}}</div>
  <div class="table" *ngFor="let row of rows">
    <ng-container [ngTemplateOutlet]="headers"></ng-container>
  </div>
</div>

I can't seem to get this working though.我似乎无法让这个工作。 Here is a sample fiddle: https://jsfiddle.net/uctxnads/这是一个示例小提琴: https://jsfiddle.net/uctxnads/

Edit: Please ensure you are using the current fiddle.编辑:请确保您使用的是当前的小提琴。

I am trying to get the content of the div.table to scroll, not .contentB .我试图让div.table的内容滚动,而不是.contentB As I wanted to have the {{title}} to remain static if at all possible.因为我想让 {{title}} 尽可能保留 static。

Your contentB class needs to have你的内容B class 需要有

    overflow-y: scroll;

This worked for me using your fiddle.这对我使用你的小提琴有用。

Edit:编辑:

For the scroll to work only within your table, you can create a parent div right before it with a class that adds a max-height and automatic scroll, like this:为了使滚动仅在您的表格中起作用,您可以在它之前创建一个父 div,使用 class 添加最大高度和自动滚动,如下所示:

    <div class="scrollable">
        <div class="table">
        <!-- contents of your table -->
        </div>
    </div>

And your scrollable class should have你的可滚动 class 应该有

        .scrollable {
           max-height: 50px; // You can set this as you wish
           overflow-y:auto;
        }

Working fiddle with this example: https://jsfiddle.net/jgztwmsk/处理这个例子: https://jsfiddle.net/jgztwmsk/

Change the CSS for .contentB to allow it to scroll:更改.contentB的 CSS 以允许它滚动:

.contentB {
  overflow-y: auto;
  height: 100%;
}

https://jsfiddle.net/fnrm4aLg/ https://jsfiddle.net/fnrm4aLg/

And if you need the .title NOT to scroll, just move it up into the previous container, out of the current one: https://jsfiddle.net/p6xr39yv/如果您需要.title不滚动,只需将其移到上一个容器中,从当前容器中移出: https://jsfiddle.net/p6xr39yv/

The problem is that you are using the calc function, use a direct value.问题是您使用的是calc function,使用直接值。

You can use overflow-y: auto;您可以使用overflow-y: auto; on .contentA ..contentA上。

And on .contentB there's no need to change the overflow..contentB上,无需更改溢出。

https://jsfiddle.net/ratyxpj7/ https://jsfiddle.net/ratyxpj7/

 .wrapper { height: 500px; width: 100%; position: absolute; }.banner { height: 100px; background-color: grey; }.contentA { height: 350px; overflow-y: scroll; }.footer { height: 50px; background-color: grey; } /* This is the content of the inner component. */.contentB { height: 100%; }.table { display: table; overflow-y: scroll; }.table > div { display: table-row; }.table div div { display: table-cell; }
 <div class="wrapper"> <div class="banner"></div> <div class="contentA"> <div class="contentB"> <div class="title"> Title </div> <div class="table"> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> <div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> <div>CellA</div> </div> </div> </div> </div> <div class="footer"></div> </div>

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

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