【问题标题】:How to make sticky table rows in Safari如何在 Safari 中制作粘性表格行
【发布时间】:2021-10-29 00:05:23
【问题描述】:

我正在尝试显示一个 table,它显示了一些分组数据。

标题应该粘贴(在视口的顶部),以及一些包含组标题的表格行。

为简单起见,我在 Codepen 上创建了以下示例: https://codepen.io/andreivictor/pen/RwZRZav

我试过的代码是:

td.sticky {
  background: red;
  color: white;
  position: sticky;
  top: 50px;  // this is the header height
}

在 Chrome 和 Firefox 上运行良好。

问题是它在 Safari 中不起作用(在 Safari v14 上测试);在 Safari 移动版中都没有。看截图: https://i.imgur.com/8NEgPYB.png - 该行是粘性的 - 但 top 的位置不同(相对于表格的顶部 - 而不是视口的顶部)。

【问题讨论】:

  • 所以只是为了确保:如果您在粘性课程中使用 top: 0 而不是 top: 50px 正在使用 safari?
  • 是的,似乎解决方案是 top: 0- 用于 safari / top: 50px - 用于其他浏览器。但这看起来像是某种黑客行为,我也试图了解为什么会在 safari 上发生这种情况。

标签: css safari mobile-safari


【解决方案1】:

根据MDN

粘性

.... 该值始终会创建一个新的堆叠上下文。

我最好的猜测是 safari 会考虑到前一个 position: sticky 元素(所以是表头)的堆叠上下文,并将 .sticky 定位在前一个位置的顶部 50px 粘性(而不是从容器的末尾)或类似的东西(我个人认为文档对此有点模糊,因此似乎没有必要错误的方法),但遗憾的是他们的行为方式不同。

This post 似乎也暗示在 Safari 上堆叠粘性定位元素有点奇怪:

...特别是 Sifiari 的双重粘性部分和元素的能力。

(Sifiari 是 Safari 的)

您可以查看最后一个链接中的讨论,但我不确定您是否会找到比使用不同的最高值更好的解决方法。您可以尝试使用类似

的选项

_::-webkit-full-page-media, _:future, :root .safari_only { ...

is there a css hack for safari only?建议的其他选项

【讨论】:

    【解决方案2】:

    也许这会有所帮助:How to fix issues with CSS position sticky

    【讨论】:

    • 请引用引用网站的相关部分。这将帮助提问者(和未来的观众)弄清楚要看什么以及如何更快地解决他们的问题。保留外部参考 - 最佳答案引用外部资源中的更多信息!更多信息在这里:stackoverflow.com/help/how-to-answer
    【解决方案3】:

    您可以为所有 safari 浏览器使用 -webkit 前缀:

       position: -webkit-sticky;
    

    table {
      border-collapse: collapse;
    }
    th,
    td {
      padding: 1rem;
    }
    thead,
    tfoot {
      background: #eee;
    }
    thead {
       position: -webkit-sticky;
      position: sticky;
      top: 0;
      border-bottom: 2px solid #ccc;
    }
    
    body {
      font: 110%/1.4 system-ui;
      margin: 0;
      padding: 10rem 2rem 50rem;
    }
    
    .sticky {
      background: red;
      color: white;
      position: -webkit-sticky;
      position: sticky;
      top: 50px;
      text-align: left;
    }
    
    .sticky--2 {
      background: indigo;
    }
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem doloribus quibusdam nostrum quo tempore veritatis magnam similique nisi quis enim soluta, cupiditate officiis. Voluptate eligendi dolor earum ipsam obcaecati? Placeat.</p>
    
    <table>
      <thead>
        <tr>
          <th>Header Cell</th>
          <th>Header Cell</th>
          <th>Header Cell</th>
          <th>Header Cell</th>
          <th>Header Cell</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <th class="sticky" colspan="5">Group Header 1 </th>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
    
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th class="sticky sticky--2" colspan="5">Group Header 2</th>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
        <tr>
          <th>Row Header</th>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
          <td>Cell Data</td>
        </tr>
      </tbody>
    </table>
    
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Exercitationem doloribus quibusdam nostrum quo tempore veritatis magnam similique nisi quis enim soluta, cupiditate officiis. Voluptate eligendi dolor earum ipsam obcaecati? Placeat.</p>

    【讨论】:

    猜你喜欢
    • 2020-08-17
    • 2020-04-03
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2017-03-23
    • 2013-04-18
    相关资源
    最近更新 更多