【问题标题】:Table with overflow auto hides content with position absolute具有溢出自动的表隐藏具有绝对位置的内容
【发布时间】:2019-06-12 17:27:13
【问题描述】:

小提琴解释了问题。我有一个可滚动的表格和其中的上下文菜单,但是使用溢出-x:自动,这个“上下文菜单”不再可见。

table {
  overflow: auto;
  width: 100%;
  display: block;
}

.absoluteElement {
  position: absolute;
}
<h2>ABSOLUTE Position Problem</h2>
<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
      <th>Column 4</th>
      <th>Column 5</th>
      <th>Column 6</th>
      <th>Column 7</th>
      <th>Column 8</th>
      <th>Column 9</th>
      <th>Column 10</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="position: absolute">
        <div class="absoluteElement">
          Content will scroll with the bar
        </div>
      </td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
      <td>9</td>
    </tr>
  </tbody>
</table>

https://jsfiddle.net/ohwy3z6L/16/

如何在表格上滚动并保持上下文菜单出现?

【问题讨论】:

    标签: html css


    【解决方案1】:

    我不确定我是否正确理解了您的问题,但这是我设法做的,如果有问题,我会尝试解决。

    tr {
      overflow: auto;
      width: 100%;
    }
    
    .relativeElement {
      position: relative;
      /* Imagine a dropdown/context menu on the grid here */
      top: 10px;
      left: 15px;
    }
    
    .absoluteElement {
      position: absolute;
      /* Imagine a dropdowncontext menu on the grid here */
      top: 10px;
      left: 15px;
    }
    <h2>RELATIVE Position Problem</h2>
    <table>
      <thead>
        <tr>
          <th>Column 1</th>
          <th>Column 2</th>
          <th>Column 3</th>
          <th>Column 4</th>
          <th>Column 5</th>
          <th>Column 6</th>
          <th>Column 7</th>
          <th>Column 8</th>
          <th>Column 9</th>
          <th>Column 10</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <div class="relativeElement">
              Content will cut here
            </div>
          </td>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
          <td>5</td>
          <td>6</td>
          <td>7</td>
          <td>8</td>
          <td>9</td>
        </tr>
      </tbody>
    </table>
    
    <h2>ABSOLUTE Position Problem</h2>
    <table>
      <thead>
        <tr>
          <th>Column 1</th>
          <th>Column 2</th>
          <th>Column 3</th>
          <th>Column 4</th>
          <th>Column 5</th>
          <th>Column 6</th>
          <th>Column 7</th>
          <th>Column 8</th>
          <th>Column 9</th>
          <th>Column 10</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td style="position: relative">
            <div class="absoluteElement">
              Content will scroll with the bar
            </div>
          </td>
          <td>1</td>
          <td>2</td>
          <td>3</td>
          <td>4</td>
          <td>5</td>
          <td>6</td>
          <td>7</td>
          <td>8</td>
          <td>9</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 嘿!感谢您的回复。我试过你的例子,但桌子上的水平滚动条没有出现。我需要在每行的第一列中有一个带有水平滚动和浮动内容的表格。但是在这种情况下,内容会因为overflow-x而隐藏,例如,甚至设置一个overflow-y: visible。你找到问题了吗?
    • 所以你想制作一个从左到右的卷轴?
    • 是的!实际上,我在这里找到了一个解决方案,它在 IE11/Firefox/Chrome 上对我有用。我所做的是从表本身中删除一个 position: relative ,将我的浮动内容与 position: absolute 和 top: auto (在 margin-top 上用负值固定顶部,以及我使用的绝对内容的父级位置:静态的,所以元素不再受滚动的影响,它又是浮动的。
    • 嗯,很有趣。使用您找到的解决方案更新问题。
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2011-06-09
    • 2017-02-26
    • 2015-09-23
    • 2011-06-04
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多