带有列和标题的可滚动表格

Posted

技术标签:

【中文标题】带有列和标题的可滚动表格【英文标题】:Scrollable table with column and header sticky 【发布时间】:2021-08-05 23:21:45 【问题描述】:

编辑:

   table 
            border: 1px solid white;
            text-align: center;
            padding: 6px;
            background: #e1edf9;
        

        td 
            border: 1px solid white;
            text-align: center;
            padding: 8px;
            min-width: 120px;
        

        td:first-child,
        th:first-child 
            background-color: #003a6a !important;
            color: white !important;

        


        @media screen and (max-width: 780px) 


            .table-scroll 
                position: relative;
                width: 100%;
                z-index: 1;
                margin: auto;
                overflow: auto;
            

            .table-scroll table 
                width: 100%;
                min-width: 1280px;
                margin: auto;
                /*   border-collapse: separate;*/
                border-spacing: 0;
            

            .header 
                top: 0;
                position: sticky;
                z-index: 10000;
            

            .side_header 
                position: sticky;
                left: 0;
                z-index: 20000;
            

            .common_header 
                top: 0;
                left: 0;
                position: sticky;
                z-index: 30000;
            
        
<div id="table-scroll" class="table-scroll">
        <table id="data" class="main-table">
            <tbody>
            <tr>
                <th class="common_header">Header-1</th>
                <th class="header">Header-2</th>
                <th class="header">Header-3</th>
                <th class="header">Header-4</th>
                <th class="header">Header-5</th>
                <th class="header">Header-5</th>
                <th class="header">Header-4</th>
                <th class="header">Header-5</th>
                <th class="header">Header-5</th>
            </tr>
            <tr>
                <td class="side_header">Cell-1</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>

            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            <tr>
                <td class="side_header">Cell-7</td>
                <td>Cell-8</td>
                <td>Cell-9</td>
                <td>Cell-10</td>
                <td>Cell-11</td>
                <td>Cell-2</td>
                <td>Cell-3</td>
                <td>Cell-4</td>
                <td>Cell-5</td>
            </tr>
            </tr>
        </tbody>
        </table>
    </div>

您好,这是我的代码 sn-p 我想创建粘性第一列和标题。基本上在移动视图中,我希望我的表格可以滚动,但第一列(第 1-101 列)和标题(标题 1-标题 4)保持粘性,以便它们始终可见。 我试图将第一行和第一列设为粘性,它适用于第一列并使其具有粘性,但它不适用于标题。有人可以帮忙吗?

table 
            border: 1px solid white;
            text-align: center;
            padding: 6px;
            background: #e1edf9;
        

        td 
            border: 1px solid white;
            text-align: center;
            padding: 6px;
        

        td:first-child,
        tr:first-child 
            background-color: #003a6a !important;
            color: white !important;


        

        .table-scroll 
            position: relative;
            width: 100%;
            z-index: 1;
            margin: auto;
            overflow: auto;
        

        .table-scroll table 
            width: 100%;
            min-width: 1280px;
            margin: auto;
            border-collapse: separate;
            border-spacing: 0;
        

        .table-wrap 
            position: relative;
        

        .table-scroll tr:first-child 
            background: #333;
            color: #fff;
            position: -webkit-sticky;
            position: sticky;
            top: 0;
        

        td:first-child 
            position: -webkit-sticky;
            position: sticky;
            left: 0;
            z-index: 2;
            background: #ccc;
        

        .table-scroll tfoot tr 
  position: -webkit-sticky;
  position: sticky;
  bottom: 0;
  background: #666;
  color: #fff;
  z-index:4;


  

@media screen and (max-width: 500px)
    td:first-child 
            position: -webkit-sticky;
            position: sticky;
            left: 0;
            z-index: 2;
            background: #ccc;
            max-width: 140px;
        
        
         tr:first-child 
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            z-index: 2;
            background: #ccc;
        
        
<div><p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p></div>
<div id="table-scroll" class="table-scroll">
  <table id="main-table" class="main-table">
        <tbody>
                <tr>
          <td><b>Headers</b></td>
                <th><b>Header1</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
                <th><b>Header2</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
                <th><b>Header3</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
        <th><b>Header4</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
            </tr>
      <tr>
                <td>Cell content1</td>
                <td>Cell content2</td>
                <td>Cell content3</td>
                <td>Cell content4</td>
                <td>Cell content5</td>
            </tr>
            <tr>
                <td>Cell content11</td>
                <td>Cell content12</td>
                <td>Cell content13</td>
                <td>Cell content14</td>
                <td>Cell content15</td>
            </tr>
            <tr>
                <td>Cell content21</td>
                <td>Cell content22</td>
                <td>Cell content23</td>
                <td>Cell content24</td>
                <td>Cell content25</td>
            </tr>
            <tr>
                <td>Cell content31</td>
                <td>Cell content32</td>
                <td>Cell content33</td>
                <td>Cell content34</td>
                <td>Cell content35</td>
            </tr>
            <tr>
                <td>Cell content41</td>
                <td>Cell content42</td>
                <td>Cell content43</td>
                <td>Cell content44</td>
                <td>Cell content45</td>
            </tr>
            <tr>
                <td>Cell content51</td>
                <td>Cell content52</td>
                <td>Cell content53</td>
                <td>Cell content54</td>
                <td>Cell content55</td>
            </tr>
            <tr>
                <td>Cell content61</td>
                <td>Cell content62</td>
                <td>Cell content63</td>
                <td>Cell content64</td>
                <td>Cell content65</td>
            </tr>
            <tr>
                <td>Cell content1</td>
                <td>Cell content2</td>
                <td>Cell content3</td>
                <td>Cell content4</td>
                <td>Cell content5</td>
            </tr>
            <tr>
                <td>Cell content11</td>
                <td>Cell content12</td>
                <td>Cell content13</td>
                <td>Cell content14</td>
                <td>Cell content15</td>
            </tr>
            <tr>
                <td>Cell content21</td>
                <td>Cell content22</td>
                <td>Cell content23</td>
                <td>Cell content24</td>
                <td>Cell content25</td>
            </tr>
            <tr>
                <td>Cell content31</td>
                <td>Cell content32</td>
                <td>Cell content33</td>
                <td>Cell content34</td>
                <td>Cell content35</td>
            </tr>
            <tr>
                <td>Cell content41</td>
                <td>Cell content42</td>
                <td>Cell content43</td>
                <td>Cell content44</td>
                <td>Cell content45</td>
            </tr>
            <tr>
                <td>Cell content51</td>
                <td>Cell content52</td>
                <td>Cell content53</td>
                <td>Cell content54</td>
                <td>Cell content55</td>
            </tr>
            <tr>
                <td>Cell content61</td>
                <td>Cell content62</td>
                <td>Cell content63</td>
                <td>Cell content64</td>
                <td>Cell content65</td>
            </tr>
            <tr>
                <td>Cell content71</td>
                <td>Cell content72</td>
                <td>Cell content73</td>
                <td>Cell content74</td>
                <td>Cell content75</td>
            </tr>
            <tr>
                <td>Cell content81</td>
                <td>Cell content82</td>
                <td>Cell content83</td>
                <td>Cell content84</td>
                <td>Cell content85</td>
            </tr>
            <tr>
                <td>Cell content91</td>
                <td>Cell content92</td>
                <td>Cell content93</td>
                <td>Cell content94</td>
                <td>Cell content95</td>
            </tr>
            <tr>
                <td>Cell content101</td>
                <td>Cell content102</td>
                <td>Cell content103</td>
                <td>Cell content104</td>
                <td>Cell content105</td>
            </tr>
        </tbody>
    </table>
</div>

【问题讨论】:

【参考方案1】:

这个解决方案可能有点复杂,需要一些改进,但它确实有效。它使用 css position: fixed 来修复标题行。这会产生一些问题,但可以通过 javascript(我使用 jQuery)来克服。

一个问题是让标题行与其他行一样宽,因为position:fixed 弄乱了这些宽度,需要通过编程设置tr 和单元格宽度来获得帮助。

由于您可能希望标题行左右滚动,我们附加一个滚动事件监听器并根据表格的滚动位置向左或向右移动标题行。

希望这对你有用!

您还可以检查插件。 This one 好像能帮上忙。

顺便说一句:你有两个开头的 &lt;tr&gt; 标签开始标题行。你应该删除一个。

var $ = jQuery;
$(function()
  //set the width of the header row
    var width = $("tr:nth-child(2)").width();
    $("tr:first-child").width(width );
  //set the width of all cells in the header row
    var cellWidth = $("tr:nth-child(2) td").width();
    $("tr:first-child td, th").width(cellWidth );
    //make sure the table goes down a bit to make sure the headeer doesn't overlap the first row
    var height = $("tr:nth-child(2)").height();
    $('#table-scroll').css("margin-top", height);
  //set table header scroll for if already scrolled when page loads
    setScroll();
  //bind an event listener to the table to detect scroll
    $('#table-scroll').scroll(function() 
        setScroll();
    );
    function setScroll()
    //get the scroll position and make sure the header goes left or right
        var currentScrollPos = $('#table-scroll').scrollLeft();
        $("tr:first-child").css("left", - currentScrollPos);
    
);
body
        margin: 0;
    
    
table 
            border: 1px solid white;
            text-align: center;
            padding: 6px;
            background: #e1edf9;
        

        td 
            border: 1px solid white;
            text-align: center;
            padding: 6px;
        

        td:first-child,
        tr:first-child 
            background-color: #003a6a !important;
            color: white !important;


        

        .table-scroll 
            position: relative;
            width: 100%;
            z-index: 1;
            margin: auto;
            overflow: auto;
        

        .table-scroll table 
            width: 100%;
            min-width: 1280px;
            margin: auto;
            border-collapse: separate;
            border-spacing: 0;
            padding:0;
            margin:0;
        

        .table-wrap 
            position: relative;
        

        .table-scroll tr:first-child 
            background: #333;
            color: #fff;
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            position:fixed;
            z-index:9999;
        

        td:first-child 
            //position: -webkit-sticky;
            //position: sticky;
            left: 0;
            z-index: 2;
            background: #ccc;
        

        .table-scroll tfoot tr 
  position: -webkit-sticky;
  position: sticky;
  bottom: 0;
  background: #666;
  color: #fff;
  z-index:4;


  

@media screen and (max-width: 500px)
    
    
    td
        //margin: 0;
        padding: 6px 0;
    
    td:first-child 
            position: -webkit-sticky;
            position: sticky;
            left: 0;
            z-index: 2;
            background: #ccc;
            //max-width: 140px;
        
        
         tr:first-child 
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            z-index: 2;
            background: #ccc;
            position:fixed;
            z-index:1;
        
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="table-scroll" class="table-scroll">
 <table id="main-table" class="main-table">
        <tbody>
            <tr>
                
          <td><b>Headers</b></td>
                <th><b>Header1</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
                <th><b>Header2</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
                <th><b>Header3</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
        <th><b>Header4</b>
                    <input type="checkbox" name="checkbox" value="checkbox">
                </th>
            
      <tr>
                <td>Cell content1</td>
                <td>Cell content2</td>
                <td>Cell content3</td>
                <td>Cell content4</td>
                <td>Cell content5</td>
            </tr>
            <tr>
                <td>Cell content11</td>
                <td>Cell content12</td>
                <td>Cell content13</td>
                <td>Cell content14</td>
                <td>Cell content15</td>
            </tr>
            <tr>
                <td>Cell content21</td>
                <td>Cell content22</td>
                <td>Cell content23</td>
                <td>Cell content24</td>
                <td>Cell content25</td>
            </tr>
            <tr>
                <td>Cell content31</td>
                <td>Cell content32</td>
                <td>Cell content33</td>
                <td>Cell content34</td>
                <td>Cell content35</td>
            </tr>
            <tr>
                <td>Cell content41</td>
                <td>Cell content42</td>
                <td>Cell content43</td>
                <td>Cell content44</td>
                <td>Cell content45</td>
            </tr>
            <tr>
                <td>Cell content51</td>
                <td>Cell content52</td>
                <td>Cell content53</td>
                <td>Cell content54</td>
                <td>Cell content55</td>
            </tr>
            <tr>
                <td>Cell content61</td>
                <td>Cell content62</td>
                <td>Cell content63</td>
                <td>Cell content64</td>
                <td>Cell content65</td>
            </tr>
            <tr>
                <td>Cell content1</td>
                <td>Cell content2</td>
                <td>Cell content3</td>
                <td>Cell content4</td>
                <td>Cell content5</td>
            </tr>
            <tr>
                <td>Cell content11</td>
                <td>Cell content12</td>
                <td>Cell content13</td>
                <td>Cell content14</td>
                <td>Cell content15</td>
            </tr>
            <tr>
                <td>Cell content21</td>
                <td>Cell content22</td>
                <td>Cell content23</td>
                <td>Cell content24</td>
                <td>Cell content25</td>
            </tr>
            <tr>
                <td>Cell content31</td>
                <td>Cell content32</td>
                <td>Cell content33</td>
                <td>Cell content34</td>
                <td>Cell content35</td>
            </tr>
            <tr>
                <td>Cell content41</td>
                <td>Cell content42</td>
                <td>Cell content43</td>
                <td>Cell content44</td>
                <td>Cell content45</td>
            </tr>
            <tr>
                <td>Cell content51</td>
                <td>Cell content52</td>
                <td>Cell content53</td>
                <td>Cell content54</td>
                <td>Cell content55</td>
            </tr>
            <tr>
                <td>Cell content61</td>
                <td>Cell content62</td>
                <td>Cell content63</td>
                <td>Cell content64</td>
                <td>Cell content65</td>
            </tr>
            <tr>
                <td>Cell content71</td>
                <td>Cell content72</td>
                <td>Cell content73</td>
                <td>Cell content74</td>
                <td>Cell content75</td>
            </tr>
            <tr>
                <td>Cell content81</td>
                <td>Cell content82</td>
                <td>Cell content83</td>
                <td>Cell content84</td>
                <td>Cell content85</td>
            </tr>
            <tr>
                <td>Cell content91</td>
                <td>Cell content92</td>
                <td>Cell content93</td>
                <td>Cell content94</td>
                <td>Cell content95</td>
            </tr>
            <tr>
                <td>Cell content101</td>
                <td>Cell content102</td>
                <td>Cell content103</td>
                <td>Cell content104</td>
                <td>Cell content105</td>
            </tr>
        </tbody>
    </table>
</div>

【讨论】:

不,它不起作用,以防我们上面有一些内容。我已经更新了代码。【参考方案2】:

我坚持列和标题应该是粘性的问题的描述。

输出图像:

这可能对你有用。

* 
  font-family: 'arial';


.container 
  max-width: 426px;
  width: 100%;
  height: 300px;
  overflow-x: auto;


table 
  border-collapse: collapse;
  width: 626px;


tr td,
tr th 
  box-shadow: 0px 0px 0px 1px white inset;
  padding: 20px 50px;


td 
  background: #e1edf9;


th 
  background: #003a6a;
  color: white;
  white-space: nowrap;


.header 
  top: 0;
  position: sticky;
  z-index: 10000;


.side_header 
  position: sticky;
  left: 0;
  z-index: 20000;


.common_header 
  top: 0;
  left: 0;
  position: sticky;
  z-index: 30000;
<div class="container">
  <table cellspacing="0">
    <tr>
      <th class="common_header">Header-1</th>
      <th class="header">Header-2</th>
      <th class="header">Header-3</th>
      <th class="header">Header-4</th>
      <th class="header">Header-5</th>
    </tr>
    <tr>
      <td class="side_header">Cell-1</td>
      <td>Cell-2</td>
      <td>Cell-3</td>
      <td>Cell-4</td>
      <td>Cell-5</td>
    </tr>

    <tr>
      <td class="side_header">Cell-7</td>
      <td>Cell-8</td>
      <td>Cell-9</td>
      <td>Cell-10</td>
      <td>Cell-11</td>
    </tr>

    <tr>
      <td class="side_header">Cell-12</td>
      <td>Cell-13</td>
      <td>Cell-14</td>
      <td>Cell-15</td>
      <td>Cell-16</td>
    </tr>

    <tr>
      <td class="side_header">Cell-17</td>
      <td>Cell-18</td>
      <td>Cell-19</td>
      <td>Cell-20</td>
      <td>Cell-21</td>
    </tr>

    <tr>
      <td class="side_header">Cell-1</td>
      <td>Cell-2</td>
      <td>Cell-3</td>
      <td>Cell-4</td>
      <td>Cell-5</td>
    </tr>

    <tr>
      <td class="side_header">Cell-7</td>
      <td>Cell-8</td>
      <td>Cell-9</td>
      <td>Cell-10</td>
      <td>Cell-11</td>
    </tr>

    <tr>
      <td class="side_header">Cell-12</td>
      <td>Cell-13</td>
      <td>Cell-14</td>
      <td>Cell-15</td>
      <td>Cell-16</td>
    </tr>

    <tr>
      <td class="side_header">Cell-17</td>
      <td>Cell-18</td>
      <td>Cell-19</td>
      <td>Cell-20</td>
      <td>Cell-21</td>
    </tr>

    <tr>
      <td class="side_header">Cell-1</td>
      <td>Cell-2</td>
      <td>Cell-3</td>
      <td>Cell-4</td>
      <td>Cell-5</td>
    </tr>

    <tr>
      <td class="side_header">Cell-7</td>
      <td>Cell-8</td>
      <td>Cell-9</td>
      <td>Cell-10</td>
      <td>Cell-11</td>
    </tr>

    <tr>
      <td class="side_header">Cell-12</td>
      <td>Cell-13</td>
      <td>Cell-14</td>
      <td>Cell-15</td>
      <td>Cell-16</td>
    </tr>

    <tr>
      <td class="side_header">Cell-17</td>
      <td>Cell-18</td>
      <td>Cell-19</td>
      <td>Cell-20</td>
      <td>Cell-21</td>
    </tr>

    <tr>
      <td class="side_header">Cell-1</td>
      <td>Cell-2</td>
      <td>Cell-3</td>
      <td>Cell-4</td>
      <td>Cell-5</td>
    </tr>

    <tr>
      <td class="side_header">Cell-7</td>
      <td>Cell-8</td>
      <td>Cell-9</td>
      <td>Cell-10</td>
      <td>Cell-11</td>
    </tr>

    <tr>
      <td class="side_header">Cell-12</td>
      <td>Cell-13</td>
      <td>Cell-14</td>
      <td>Cell-15</td>
      <td>Cell-16</td>
    </tr>

    <tr>
      <td class="side_header">Cell-17</td>
      <td>Cell-18</td>
      <td>Cell-19</td>
      <td>Cell-20</td>
      <td>Cell-21</td>
    </tr>
  </table>
</div>

【讨论】:

谢谢我已经根据您的更改编辑了我的代码,但标题仍然没有得到修复。 @sd_30 你希望标题在水平或垂直滚动​​时固定吗? 双向。垂直滚动时,标题应固定,水平滚动时,第一列也应固定 @sd_30 当它水平滚动时,您希望第一列的标题以及该列被固定 就像您发布的 sn-p 看起来不错,但我无法在我的代码中实现相同的效果。 ps:我根据您的更改发布了编辑【参考方案3】:

.sticky 
  position: -webkit-sticky;
  position: sticky;
  top: 0;
<nav class="sticky">Placeholder text<!-- Anything in here will stick and stay on-screen --></nav>
<p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p><p>Dummy text to enable scrolling...</p>

【讨论】:

以上是关于带有列和标题的可滚动表格的主要内容,如果未能解决你的问题,请参考以下文章

如何使用角度和离子制作带有固定标题的可滚动表格

具有固定页眉、第一列和页脚的表格 + 向内滚动

带圆角的可滚动表格

使用 Reactstrap 响应式表的带有固定标题的可滚动表

css让表格第一列和第一行固定

Flexbox 类型的固定表头,表格标记的可滚动内容