【问题标题】:CSS Grid Layout in Chrome seems not to work properly with more than 1000 rowsChrome 中的 CSS 网格布局在超过 1000 行时似乎无法正常工作
【发布时间】:2021-09-12 18:11:51
【问题描述】:

我使用“CSS Grid Layout”和“sticky position”技术创建了一个带有固定标题的滑动网格示例。为方便起见,网格的内容是通过脚本生成的,我觉得效果不错。

function fillGrid(selector, rows) {
  let cols = 3;
  let grid = $(selector);
  
  grid.empty();
  
  //cr header
  grid.append($('<div>').addClass('hcr').text('#'));
  
  //col headers
  for (let c = 1; c <= cols; c++) {
    grid.append($('<div>').addClass('hc').text(`Column ${c}`));
  }
  
  for (let r = 1; r <= rows; r++) {
    //row header
    grid.append($('<div>').addClass('hr').text(r));
    
    //cells
    for (let c = 1; c <= cols; c++) {
      grid.append($('<div>').addClass('c').text(`Cell ${r}-${c}`));
    }
  }
}

$('#reload').click(e => {
  var rows = Number.parseInt($('#rows').val());
  fillGrid('#grid1', rows);
})

$(document).ready(function() {
  fillGrid('#grid1', 10);
});
body {
  font-family: 'Segoe UI', sans-serif;
  font-size: 12px;
}

.grid {
  display: grid;
  width: 600px;
  height: 300px;
  grid-template-columns: 40px 200px 100px 500px;
  grid-auto-rows: min-content;
  border: 1px solid #ccc;
  overflow: scroll;
  margin-top: 20px;
  background-color: #aaa;
  margin-right: 10px;
}

.hcr, .hc, .hr {
  background-color: #ddd;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  padding: 2px;
  position: sticky;
}

.hcr {
  top: 0;
  left: 0;
  z-index: 1;
  text-align: center;
}

.hc {
  top: 0;
  white-space: nowrap;
}

.hr {
  left: 0;
  text-align: center;
}

.c {
  padding: 2px;
  border-right: 1px solid #ccc;
  border-bottom: 1px solid #ccc;
  background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="text" id="rows" value="10" />
  <input type="button" id="reload" value="Reload" />
</div>
<div class="grid" id="grid1"></div>

网格最多可显示 999 行,完美运行。加载超过 999 行时,仅显示第 999 行之前的单元格,而以下单元格错误地位于第 999 行标题上方的左侧。

相同的示例在 Firefox 56 和 Edge 16(版本 16299)中正常工作。

我哪里错了?

【问题讨论】:

    标签: html css google-chrome css-grid


    【解决方案1】:

    好的,出于稳定性和 RAM 消耗的原因,Chrome 引擎有意引入了 1000 行(以及 1000 列)限制。网格功能的新版本似乎正在开发中,应该可以解决问题。

    来源:

    UPD:从 Chrome 版本 96.0.4642 扩展至 100,000 行/列

    【讨论】:

      【解决方案2】:

      我制作了一支笔,可以解决这个问题:10K Rows CSS Grid Table

      简而言之 - 解决方案是仅根据滚动位置呈现可见行。 不可见的行应替换为接收到的单个“间隙填充”行 他们的总高度。这种技术称为虚拟化窗口化

      为了使其“乐观”,该空白填充行还应该接收一个渐变背景,模拟水平行线条,使其看起来好像线条在那里(因为当用户滚动和滚动时,该行将短暂可见我们不希望它是空白的)。

      在性能方面,应用此解决方案时,100 行的表与 10K 行的表的性能完全相同。

      例如:

      <div class="table">
        <div class="gap-before" 
             style="height: {{total height of rows before the visible rows}}">
        <!-- visible rows go here -->
        <div class="gap-after" 
             style="height: {{total height of rows after the visible rows}}">
      </div>
      

      【讨论】:

        【解决方案3】:

        根据您的布局,您还可以将display: grid 替换为display: block

        如果您的表格中有多个列,那么您可能需要为每一行包装元素,但它对我来说非常好。

        可以使用 margin-top 再现网格间隙。

        【讨论】:

          【解决方案4】:

          Yoav Kadosh 的回答是一个不错的方法,但我使用了两个滚动区域,因此我不必处理滚动去抖动器。

          <style>
          #cnt {
              position: relative;
              width: fit-content;
          }
          #main {
              position: relative;
          }
          #scroll,
          #sp {
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              overflow: auto;
              width: auto;
              max-width: fit-content;
          }
          .grid_cnt {
              display: grid;
              width: fit-content;
          }
          </style>
          <div id="main" style="height: 1130.54px;">
            <div id="sp">
              <div id="cnt">
                <div class="grid_cnt" style="grid-template-columns: auto auto auto auto auto auto;">
                  <div id="top_gap" style="height: 2507px; width: 1px; grid-column: 1 / 7;"></div>
              <!--  grid cells -->
                  <div id="bottom_gap" style="height: 828px; width: 1px; grid-column: 1 / 7;"></div>
                </div>
              </div>
            </div>
            <div id="scroll">
              <div id="scroll_sheet" style="height: 4624px; width: 574px;"></div>
            </div>
          </div>
          

          【讨论】:

            猜你喜欢
            • 2019-08-09
            • 1970-01-01
            • 1970-01-01
            • 2020-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多