【问题标题】:Freeze the first two rows on vertical scrolling of the page在页面垂直滚动时冻结前两行
【发布时间】:2019-12-03 16:11:17
【问题描述】:

我已经实现了一个带有角度水平滚动的表格,并且需要在垂直滚动时修复前两列。我创建了一个 stackblitz 来显示两个表。我要寻找的是,当用户滚动第二个表格时,应该固定前两行,即 Legal Class Name 和 Fund Name。

我尝试将以下类应用于各自的 tds,但没有成功

.stickyRows {
    position: sticky;
}

这里是堆栈闪电战

https://stackblitz.com/edit/angular-o2ukfs

        th {
            padding: 7px;
            position: sticky;
            left: 0px;
            min-width: 250px;
            width: 250px;
            background-color: #f5f7f7;
        }
    
    
        td {
            padding: 7px;
            min-width: 250px;
            /* max-width: 300px; */
        }
    
        .fundClassesTable {
    
            table-layout: fixed;
        }
    
    
        .cellbgcolor {
            color: transparent;
        }
    
        .btn {}
    
        .tableItem {
            text-align: left;
            border-left: solid 1px lightgrey;
            border-top: solid 1px lightgrey;
            border-right: solid 1px lightgrey;
            border-bottom: solid 1px lightgrey;
    
        }
    
        .rowItem:hover {
            background-color: #f5f7f7;
        }
    
    
        label {
            margin-left: 0.5rem;
            vertical-align: middle
        }
    
    
        .panel-heading {
            color: black;
            border-color: #ddd;
            overflow: hidden;
            padding-top: 5px !important;
            padding-bottom: 5px !important;
        }
    
        .panel-heading .left-label {
            display: inline-block;
            padding-top: 5px !important;
    
        }
    
        .scrollClass {
            overflow-x: scroll;
            display: grid;
            overflow-y:hidden;
            height: 100%;
        }
    
        .panel-heading label {
            margin-bottom: 0px !important;
        }
    
        #FundClass tr:hover {
            background-color: #ECF0F1;
        }
    
        .headcol {
            position: absolute;
            min-width: 300px;
            max-width: 300px;
            width: 5em;
            left: 0;
            top: auto;
            border-top-width: 1px;
            /*only relevant for first row*/
            margin-top: -1px;
            /*compensate for top border*/
        }
    
        .headcol:before {
            content: 'Row ';
        }
    
        .collapsed {
            color: #d6630a;
            font-size: 22px;
            text-decoration: none;
            font-weight: bold;
        }
    
        .expanded {
            color: #d6630a;
            font-size: 22px;
            text-decoration: none;
            font-weight: bold;
    
        }
    
        .fixed-side {
            border: 1px solid #000;
            background: #eee;
            visibility: visible;
        }
    
    
        .card-body {
            flex: 1 1 auto;
            padding: 0px;
            margin: 10px 0;
            background-color: white;
    
        
        }
    
       
    <div class="card">
    	<div class="card-header panel-heading">
    		<span class="left-label" style="font-size: 18px; font-weight: bold; ">Accounting Fund Classes</span>
          
        </div>
    
    
        <div [ngClass]="{'show': isExpanded}" id="fundClass" class="collapse" role="tabpanel" aria-labelledby="fundClass_heading"
            data-parent="#fundClass" [attr.aria-expanded]="isExpanded">
            <div class="card-body scrollClass" *ngIf="data">
    
                <table id="FundClass" class="fundClassesTable table-striped">
                    <tr *ngFor="let c of ColumnNames">
                        <th Fund Name scope="col" [ngClass]="c != 'Buttons1'? 'tableItem bold' : 'tableItem cellbgcolor'"> {{ c }}</th>
                        <ng-container *ngFor="let f of data let i=index">
                           
                                <td class="tableItem" style="font-weight: bold"  *ngIf="c == 'Fund Name'">
                                    {{ f.FundName}}
                                </td>
                                 <td [attr.id]="'f.Id'" *ngIf="c == 'Accounting Class Name'"
                                    class="tableItem">
                                    {{ f.FundName}}
                                </td>
                                <td class="tableItem" *ngIf="c == 'Class ID'">{{f.Id}}</td>
                               
                                <td [attr.id]="'f.Id'" *ngIf="c == 'Legal Fund Class'"
                                    class="tableItem">
                                    {{ f.LegalFundClassName}}
                                </td>
                               
                                <td [attr.id]="'f.Id'" *ngIf="c == 'Invested Amount'"
                                    class="tableItem">
                                    {{ f.InvestedAmount | number : '.2-2'}}
                                </td>
                               
                                <td [attr.id]="'f.Id'" *ngIf="c == 'Vehicle Type'"
                                    class="tableItem">
                                    {{ f.VehicleTypeName}}
                                </td>
                               
                                <td [attr.id]="'f.Id'" *ngIf="c == 'Closure Status'"
                                    class="tableItem">
                                    {{ f.ClosureStatusName}}
                                </td>
                               
                              
    
                                <td class="tableItem" *ngIf="c == 'Buttons1'">
    
                                    <button *ngIf="!EditMode[f.Id]" type="button"
                                        class="btn btn-primary btn mr-1 "
                                        (click)="buttonClicked(f.Id)">Edit</button>
                                    
                                    <button *ngIf="EditMode[f.Id]" type="button"
                                        class="btn btn-primary btn mr-1"
                                        (click)="buttonClicked(f.Id)">Cancel</button>
    
                                </td>
    
                          
                        </ng-container>
    
                    </tr>
                </table>
            </div>
        </div>
    </div>



    

【问题讨论】:

  • 要记住的一点:根据MDN,IE 和Safari 不支持“作为sticky 定位容器的表格元素”。也许在那里,Ricardo 的 Flexbox 方法将在 position:sticky 上占据优势?
  • 我创建了一个新的 stackbltiz。实际上我需要冻结第二个表的前两行而不是第一行。 stackblitz.com/edit/angular-o2ukfs
  • 行法律类别名称和基金名称

标签: html css angular


【解决方案1】:

尝试将此添加到您的样式中


  .card-body{
    height:200px;
    position:relative!important;
    overflow-y:scroll;
  }


  table tr:nth-child(1){

    position :sticky;
    top:0;
    z-index:999;
    left:0;
    background-color:#fff;
}

 table tr:nth-child(2){

    position :sticky;
    top:35px;
    z-index:999;
    background-color:#fff;
    left:0;
}

【讨论】:

  • 这不是我想要的。列标题也是滚动的而不是固定的。我正在考虑根据浏览器栏的滚动来固定列标题
  • 或者,如果在卡片级别使用滚动条,至少我正在查看正在修复的标题
  • 如果您可以在 stackblitz 上尝试您的解决方案,那就太好了
  • 我已经用新的 stackblitz 更新了帖子。查看第二个 table.rows 法律类名称和基金名称的解决方案
【解决方案2】:

我希望这就是您想要的:

app.component.css

/* Container should define how tall the scrollable content is */
.scrollClass{
  height: 500px;
}

/* tagetting the first <th>; to ensure <th> are scrolled along with <td> */
.fundClassesTable tr:nth-child(1) th{
  z-index: 3;
  position: sticky;
  position: -webkit-sticky;
  top: 2px;
}

/* target all <td> in the first row to be sticky */
.fundClassesTable tr:nth-child(1) td{
  color: red;
  position: sticky;
  position: -webkit-sticky;
  top: 2px;
  z-index: 2;
  background-color: white;
  font-weight: bold;
}

/* Same as above but with top property as 36px 
because the 2nd "row" is 36px from the top of <table> */
.fundClassesTable tr:nth-child(2) th{
  z-index: 3;
  position: sticky;
  position: -webkit-sticky;
  top: 38px;
}

.fundClassesTable tr:nth-child(2) td{
  color: red;
  position: sticky;
  position: -webkit-sticky;
  top: 38px;
  z-index: 2;
  background-color: white;
  font-weight: bold;
}

Forked Stackblitz

编辑:

Riv 的解决方案对我有用,但我遇到了一个棘手的问题。当我滚动浏览固定行时,我可以看到在固定行的间隙和固定行的边框之间可见的行数据不可见

可能不是最好的解决方案,但我会使用 ::after 伪选择器为粘贴的表格单元格创建背景,以便在向下滚动时隐藏背景单元格。

.fundClassesTable tr:nth-child(1)::after{
  content: '';
  position: absolute;
  height: 71px;
  width: 96.7%;
  top: 55px;
  left: 19px;
  z-index: 1;
  background-color: white; //create a white background to cover your other cells when scrolled down
  border: solid 1px deeppink;
}

【讨论】:

  • 我在 Windows 上使用 ShareX。如果我在 linux 上,我使用Peek。两者都是开源的屏幕捕获工具,可以立即创建 GIF。与他人合作时交流想法的真正好工具 =)
  • 请注意sticky 值在 Safari 中尚不支持(如果有人仍然关心的话,还有 IE)。
  • 嗨 samuel,你是对的,Safari 不支持 sticky,但 Safari 可以使用前缀为 position: -webkit-sticky 的供应商。我已经在我的 ipad 的 safari 中对其进行了测试,它可以正常工作。供参考,caniuse.com/#search=sticky。不幸的是,仍然不支持 IE。
  • 谢谢,这是我一直在寻找的,但在我的情况下,滚动粘性行时边框会消失。任何想法
  • 边框消失了?你的意思是在我分叉的堆栈闪电战中还是你的项目中?请检查您是否有冲突的样式规则。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2021-11-19
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多