【问题标题】:Prevent table header from scrolling vertically (to work in IE 11)防止表格标题垂直滚动(在 IE 11 中工作)
【发布时间】:2022-02-07 15:28:24
【问题描述】:

我在我的 Windows 应用程序中一直使用 IE 11 作为渲染引擎。我知道有很多示例如何防止表格标题垂直滚动,但我似乎无法让它们在 IE 中工作。有什么办法让它在那里工作吗?

这是我用于表格的 HTML 示例:

<table width='100%' border='0' cellpadding='0' cellspacing='0'>
 <thead>
  <tr>
   <th scope='col'>Col 1</th>
   <th scope='col'>Col 2</th>
   <th scope='col'>Col 3</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>---</td>
   <td>---</td>
   <td>---</td>
   <td>---</td>
  </tr>
  <tr>
   <td>+++</td>
   <td>+++</td>
   <td>+++</td>
   <td>+++</td>
  </tr>
 </tbody>
</table>

我需要确保thead 保持在表格顶部,无论其中的行列表有多长。

【问题讨论】:

    标签: css html-table internet-explorer-11


    【解决方案1】:

    我搜索了类似的线程,发现this solution 在 IE 11 中有效。它使用另一个具有固定位置的表格在滚动时在页面顶部显示标题。

    您可以参考下面的示例代码。它在 IE 11 中运行良好:

    var tableOffset = $("#table-1").offset().top;
    var $header = $("#table-1 > thead");
    var $fixedHeader = $("#header-fixed").append($header.clone());
    
    $(window).bind("scroll", function() {
      var offset = $(this).scrollTop();
    
      if (offset >= tableOffset && $fixedHeader.is(":hidden")) {
        $fixedHeader.show();
    
        $.each($header.find('tr > th'), function(ind, val) {
          var original_width = $(val).width();
          $($fixedHeader.find('tr > th')[ind]).width(original_width);
        });
      } else if (offset < tableOffset) {
        $fixedHeader.hide();
      }
    });
    #header-fixed {
      position: fixed;
      top: 0px;
      display: none;
      background-color: white;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <table width='100%' border='0' cellpadding='0' cellspacing='0' id="table-1">
      <thead>
        <tr>
          <th scope='col'>Col 1</th>
          <th scope='col'>Col 2</th>
          <th scope='col'>Col 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
        <tr>
          <td>---</td>
          <td>---</td>
          <td>---</td>
        </tr>
        <tr>
          <td>+++</td>
          <td>+++</td>
          <td>+++</td>
        </tr>
      </tbody>
    </table>
    <table id="header-fixed"></table>

    【讨论】:

    • 谢谢。但它需要 JQuery,哈?
    • @c00000fd 我测试了许多解决方案,包括粘性 polyfills,但只有这种方式在 IE 11 中效果很好。在其他解决方案中,表格标题的宽度会在滚动后发生变化。此外,IE 11 将于 2022 年 6 月 15 日停用并停止支持。我建议您可以切换到现代浏览器,不要花太多时间修复 IE 11 问题。
    • 感谢您这样做。至于不使用 IE,正如我在原帖中提到的,它被用作 Windows 桌面应用程序的一部分。它被称为“网络浏览器控制”,我不能使用其他任何东西,因为没有其他东西可用。同样对于“退役IE”,微软也做不到。 “退休”部分适用于公众,以便他们停止使用它进行网页浏览。否则,它会深深地融入操作系统(Windows)中,并且退休意味着 Windows 中的很多东西都将停止工作。所以这永远不会发生。但我同意这是一个糟糕的渲染引擎。
    • 是的,你是对的。停用仅适用于某些版本的 Windows 上的 IE 11 桌面应用程序。 WebBrowser 控件仍然可以使用。我不确定您是否可以使用其他渲染引擎。如果可以的话,我认为WebView2 control 是一个不错的选择。它使用 Edge(chromium) 作为渲染引擎。
    • 谢谢。 Tbh,我无法忍受 Chromium。真是太浪费资源了。
    【解决方案2】:

    因此,您可以做的是为表格本身分配一个相对位置,然后将 position:sticky 值应用于表格头类。

    这里有一篇文章,我觉得也可以提供更多的价值https://css-tricks.com/position-sticky-and-table-headers/

    将来,如果我们能看到您正在使用的 CSS 也会有所帮助;这样我们就可以让您知道您是否走在正确的轨道上。

    【讨论】:

    • IE 不支持粘性 (caniuse.com/css-sticky),但它是查找 polyfills 的一个很好的搜索词,因为它似乎是需要的。
    • @Sarah 谢谢。你能详细说明你的意思吗? @-ThisIsGravy - 没有与这个问题相关的 CSS。我正在寻找一个让表头保持在顶部的方法。
    • @c00000fd,这意味着 CSS 不能在您正在编码的浏览器上使用。 Sticky 可用于 Edge、Firefox、Chrome、Safari、Opera、IOS Safari、Android 浏览器、Opera Mobile(以及其他一些)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多