我有一个Electron 表格式的网格.根看起来像这样:

<div id="grid">
  <div id="left-column"></div>
  <div id="center-column"></div>
  <div id="right-column"></div>
<div>

左列和右列各为1列,但中间列为N列.

当向左和向右滚动时,我希望将左右两列锁定在适当的位置,以便它们始终可见.并且只滚动-x中间的列.

然而,当垂直滚动时,我希望锁定所有列中的第一个单元格(标题单元格),以便它们始终可见.

我有一个基本奏效的方法:

// JS is only used to generate the cells

const cells = Array.from({ length: 20 }).map(c => c = `${Math.floor(Math.random() * 200) + 50}px`)
cells[0] = '120px'
cells[19] = '120px'

const first = cells.shift()
const last = cells.pop()

const rows = Array.from({ length: 100 })

const grid = document.getElementById("grid")
const leftColumn = document.getElementById("left-column")
const centerColumn = document.getElementById("center-column")
const rightColumn = document.getElementById("right-column")

grid.style.display = "grid"
grid.style.gridTemplateColumns = `${first} 1fr ${last}`

function makeItem(text) {
  const div = document.createElement("div")
  div.classList.add('cell')
  const newContent = document.createTextNode(text);
  div.appendChild(newContent);
  return div;
}

function makeRow() {  
  const innerGrid = document.createElement("div")
  innerGrid.style.display = "grid"
  innerGrid.style.gridTemplateColumns = cells.join(' ')
  
  cells.forEach((cell, index) => {
    const child = makeItem(`cell ${index + 1}`)
    innerGrid.appendChild(child)
  })
  
  const a = makeItem('First')
  const z = makeItem('Last')
  
  leftColumn.appendChild(a)
  centerColumn.appendChild(innerGrid)
  rightColumn.appendChild(z)
}

rows.forEach(() => {
  makeRow()
})
.cell {
  background: #eee;
  font-family: Arial;
  border: 0.5px solid black;
  color: #333;
  height: 30px;
  display: grid;
  align-items: center;
  text-align: center;
}

#grid {
  width: min(100%, 1000px);
  max-height: 300px;
  overflow: scroll;
  position: relative;
  border: 0.5px solid black;
}
#center-column {
  overflow-x: scroll;
}
#left-column > .cell, #right-column > .cell, #center-column > :first-child > .cell {
  font-weight: bold;
  color: black;
  background: #aaa;
  color: #eee;
}

#left-column > :first-child,
#right-column > :first-child,
#center-column > :first-child,
#center-column > :first-child > .cell{
  position: sticky;
  top: 0;
  background: #d70000;
}
#center-column {
  position: relative;
}
<div id="grid">
  <div id="left-column"></div>
  <div id="center-column"></div>
  <div id="right-column"></div>
<div>

但是,当您垂直滚动时,左列和右列标题保持锁定,而中心标题不锁定.

我知道为什么.这是因为为了实现我想要的水平滚动行为,我需要使中间的列具有溢出滚动.这消除了使页眉锁定在适当位置的粘性位置(仅用于中心列).

所以我知道导致它不工作的问题.但我找不到一种纯粹的css解决方案.我可以使用JavaScript来实现这一点,但我正在try 寻找一种纯CSS解决方案.

我可以使用任何嵌套的html元素和样式需要我只是想要一个纯css的解决方案.

推荐答案

您不需要创建物理包装器div.只需创建您的单元格,将它们放在您的#网格元素中,并使用css和:nth-child()伪类 Select 器,然后创建所需的COL,第sticky行分别位于topleftright.

下面是一个6列轴网的示例:

// This JS is only used to dynamically generate the cells.

const el = (sel, par) => (par || document).querySelector(sel);
const elNew = (tag, prop) => Object.assign(document.createElement(tag), prop);
const repeat = (n, cb) => [...Array(n)].forEach((_, i) => cb(i));

const elGrid = el("#grid");
repeat(90, (i) => {
   elGrid.append(elNew("div", {
     className: "cell",
     textContent: `Cell ${i+1}`
   }));
});
body {
  font-family: Arial;
}

#grid {
  max-height: 180px;
  overflow-y: scroll;
  position: relative;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  
  .cell {
    border: 1px solid black;
    text-align: center;
    padding: 0.5rem;
    min-width: 20vw;
    
    /* leftmost column */
    &:nth-child(6n-5) {
      background: #ddd;
      position: sticky;
      left: 0;
    }
    
    /* rightmost column */
    &:nth-child(6n) {
      background: #ddd;
      position: sticky;
      right: 0;
    }
    
    /* top row */
    &:nth-child(-n+6) {
      background: #aaa;
      position: sticky;
      top: 0;
      z-index: 1;
    }
    
    /* first cell */
    &:nth-child(1) {
      z-index: 2;
    }

  }
}
<div id="grid"></div>

Html相关问答推荐

HTML横幅未正确对齐页面顶部

Blazor中有1像素高行的表格

我似乎不能正确地将我的导航栏居中'

UseEffect()从不调用preact

当使用浮动标签和大小时,如何在 Select 组件中有更多可见选项?

Angular 15 p-对话框不使用组件 Select 器呈现HTML

在一行中对齐文本和图标

CSS Grid:使网格行填充屏幕,将下一行推离屏幕

如何将文本环绕心形(而不仅仅是圆形)?

如何使 CSS 网格项目像 Flexbox 项目一样匹配父级的高度

标题在实时网站上闪烁

用由文本制成的边框包围内容

为什么可滚动弹性项目需要 flex-basis: 0 ?

如何用js和CSS在两点之间画一条线

在HTML请求中添加源URL

如何使用jQuery将我制作的通用头部和底部加载到多个HTML文件中?

Tailwind:父母动态适应子元素的身高

显示填充正方形的图形的简单页面的 HTML 代码

圆形边框显示在该部分后面.怎么修?

使用 golem 框架在shiny 的应用程序中存储背景图片的位置