Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有固定标题的HTML表格?_Javascript_Css_Html Table - Fatal编程技术网

Javascript 有固定标题的HTML表格?

Javascript 有固定标题的HTML表格?,javascript,css,html-table,Javascript,Css,Html Table,是否有一种跨浏览器CSS/JavaScript技术来显示一个长HTML表,这样列标题在屏幕上保持不变,而不会随表体滚动。想想Microsoft Excel中的“冻结窗格”效果 我希望能够滚动浏览表格的内容,但始终能够看到顶部的列标题。两个div,一个用于标题,一个用于数据。使数据div可滚动,并使用JavaScript将标题中列的宽度设置为与数据中的宽度相同。我认为数据列的宽度应该是固定的,而不是动态的。我刚刚完成了一个jQuery插件的组装,该插件将使用有效的HTML(必须有一个thead和t

是否有一种跨浏览器CSS/JavaScript技术来显示一个长HTML表,这样列标题在屏幕上保持不变,而不会随表体滚动。想想Microsoft Excel中的“冻结窗格”效果


我希望能够滚动浏览表格的内容,但始终能够看到顶部的列标题。

两个div,一个用于标题,一个用于数据。使数据div可滚动,并使用JavaScript将标题中列的宽度设置为与数据中的宽度相同。我认为数据列的宽度应该是固定的,而不是动态的。

我刚刚完成了一个jQuery插件的组装,该插件将使用有效的HTML(必须有一个thead和tbody)获取有效的单个表,并将输出一个具有固定页眉的表,可选的固定页脚可以是克隆的页眉,也可以是您选择的任何内容(分页等)。如果要利用较大的监视器,则在调整浏览器大小时,它也会调整表格的大小。另一个新增功能是,如果表格列不能全部显示在视图中,则可以侧滚

在github上:

它非常容易安装,你可以为它创建自己的自定义样式。它在所有浏览器中都使用圆角。请记住,我刚刚发布了它,所以它在技术上仍然是测试版,我正在解决的小问题很少


它在InternetExplorer7、InternetExplorer8、Safari、Firefox和Chrome中工作。

我寻找了一段时间的解决方案,发现大多数答案都不起作用或不适合我的情况,所以我用jQuery编写了一个简单的解决方案

这是解决方案大纲:

  • 克隆需要固定标题的表,并放置 克隆副本位于原始副本之上
  • 从顶桌上拆下桌体
  • 从底部表格中删除表格标题
  • 调整列宽。(我们跟踪原始列宽)
  • 下面是可运行演示中的代码

    函数scrolify(tblAsJQueryObject,高度){
    var oTbl=tblAsJQueryObject;
    //对于非常大的表,可以删除下面的四行
    //并在标记和分配中用
    //高度和溢出特性
    var oTblDiv=$(“”);
    oTblDiv.css(“高度”,高度);
    css('overflow','scroll');
    oTbl.包装(oTblDiv);
    //保存原始宽度
    oTbl.attr(“数据项原始宽度”,oTbl.width());
    oTbl.find('thead tr td')。每个(函数(){
    $(this.attr(“数据项原始宽度”,$(this.width());
    });
    oTbl.find('tbody tr:eq(0)td')。每个(函数(){
    $(this.attr(“数据项原始宽度”,$(this.width());
    });
    //克隆原始表
    var newTbl=oTbl.clone();
    //从原始表格中删除表格标题
    oTbl.find('thead tr').remove();
    //从新表中删除表体
    newTbl.find('tbody tr').remove();
    oTbl.parent().parent().prepend(newTbl);
    新包装(“”);
    //替换原始列宽
    newTbl.width(newTbl.attr('data-item-original-width');
    newTbl.find('thead tr td')。每个(函数(){
    $(this.width($(this.attr(“数据项原始宽度”));
    });
    oTbl.width(oTbl.attr('data-item-original-width');
    oTbl.find('tbody tr:eq(0)td')。每个(函数(){
    $(this.width($(this.attr(“数据项原始宽度”));
    });
    }
    $(文档).ready(函数(){
    scrolify($('tblneedscrolling'),160)//160是高度
    });
    
    标题1标题2
    第1行第1单元格第1行第2单元格
    第2行第1单元格第2行第2单元格
    第3行第1单元格第3行第2单元格
    第4行第1单元格第4行第2单元格
    第5行第1单元格第5行第2单元格
    第6行第1单元格第6行第2单元格
    第7行第1单元格第7行第2单元格
    第8行第1单元格第8行第2单元格
    
    使用最新版本的jQuery,并包含以下JavaScript代码

    $(window).scroll(function(){
      $("id of the div element").offset({top:$(window).scrollTop()});
    });
    
    一个简单的jQuery插件 这是Mahes解决方案的一个变体。您可以像
    $('table#foo')那样调用它。scrollableTable();

    这个想法是:

    • thead
      tbody
      拆分为单独的
      表格
      元素
    • 使它们的单元格宽度再次匹配
    • 将第二个
      表格
      包装在一个
      div.scrollable
    • 使用CSS使
      div.scrollable
      实际滚动
    CSS可以是:

    div.scrollable { height: 300px; overflow-y: scroll;}
    
    警告
    • 显然,拆分这些表会降低标记的语义。我不确定这对可访问性有什么影响
    • 这个插件不处理页脚、多个页眉等
    • 我只在Chrome版本20中测试过它
    也就是说,它适用于我的目的,你可以自由选择和修改它

    以下是插件:

    jQuery.fn.scrollableTable = function () {
      var $newTable, $oldTable, $scrollableDiv, originalWidths;
      $oldTable = $(this);
    
      // Once the tables are split, their cell widths may change. 
      // Grab these so we can make the two tables match again.
      originalWidths = $oldTable.find('tr:first td').map(function() {
        return $(this).width();
      });
    
      $newTable = $oldTable.clone();
      $oldTable.find('tbody').remove();
      $newTable.find('thead').remove();
    
      $.each([$oldTable, $newTable], function(index, $table) {
        $table.find('tr:first td').each(function(i) {
          $(this).width(originalWidths[i]);
        });
      });
    
      $scrollableDiv = $('<div/>').addClass('scrollable');
      $newTable.insertAfter($oldTable).wrap($scrollableDiv);
    };
    
    jQuery.fn.scrollableTable=函数(){
    var$newTable、$oldTable、$scrollableDiv、原始宽度;
    $oldTable=$(此);
    //拆分表格后,其单元格宽度可能会发生变化。
    //拿着这些,我们可以让这两张桌子再次匹配。
    originalWidths=$oldTable.find('tr:first td').map(函数(){
    返回$(this.width();
    });
    $newTable=$oldTable.clone();
    $oldTable.find('tbody').remove();
    $newTable.find('thead').remove();
    $.each([$oldTable,$newTable],函数(索引,$table){
    $table.find('tr:first td')。每个(函数(i){
    $(此).width(原始宽度[i]);
    });
    });
    $scrollableDiv=$('').addClass('scrollable');
    $newTable.insertAfter($oldTable.wrap($scrollableDiv);
    };
    
    这是一个用于固定表格标题的jQuery插件。它允许整个页面滚动,当到达顶部时冻结标题。它与Twitter表格配合得很好

    GitHub存储库:

    它不仅仅滚动表格内容。请查看其他工具,作为其他答案之一。您可以决定最适合您的情况。

    支持固定页脚 我扩展了Nathan的函数以支持固定页脚和最大高度。 此外,该函数将设置
    $('table').scrollableTable({ height: 100 });
    
    $('table').scrollableTable({ maxHeight: 100 });
    
    jQuery.fn.scrollableTable = function(options) {
    
        var $originalTable, $headTable, $bodyTable, $footTable, $scrollableDiv, originalWidths;
    
        // Prepare the separate parts of the table
        $originalTable = $(this);
        $headTable = $originalTable.clone();
    
        $headTable.find('tbody').remove();
        $headTable.find('tfoot').remove();
    
        $bodyTable = $originalTable.clone();
        $bodyTable.find('thead').remove();
        $bodyTable.find('tfoot').remove();
    
        $footTable = $originalTable.clone();
        $footTable.find('thead').remove();
        $footTable.find('tbody').remove();
    
        // Grab the original column widths and set them in the separate tables
        originalWidths = $originalTable.find('tr:first td').map(function() {
            return $(this).width();
        });
    
        $.each([$headTable, $bodyTable, $footTable], function(index, $table) {
            $table.find('tr:first td').each(function(i) {
                $(this).width(originalWidths[i]);
            });
        });
    
        // The div that makes the body table scroll
        $scrollableDiv = $('<div/>').css({
            'overflow-y': 'scroll'
        });
    
        if(options.height) {
            $scrollableDiv.css({'height': options.height});
        }
        else if(options.maxHeight) {
            $scrollableDiv.css({'max-height': options.maxHeight});
        }
    
        // Add the new separate tables and remove the original one
        $headTable.insertAfter($originalTable);
        $bodyTable.insertAfter($headTable);
        $footTable.insertAfter($bodyTable);
        $bodyTable.wrap($scrollableDiv);
        $originalTable.remove();
    };
    
    Putting some text here to differentiate between the header
    aligning with the top of the screen and the header aligning
    with the top of one of its ancestor containers.
    
    <div id="positioning-container">
    <div id="scroll-container">
        <table>
            <colgroup>
                <col class="col1"></col>
                <col class="col2"></col>
            </colgroup>
            <thead>
                <th class="header-col1"><div>Header 1</div></th>
                <th class="header-col2"><div>Header 2</div></th>
            </thead>
            <tbody>
                <tr><td>Cell 1.1</td><td>Cell 1.2</td></tr>
                <tr><td>Cell 2.1</td><td>Cell 2.2</td></tr>
                <tr><td>Cell 3.1</td><td>Cell 3.2</td></tr>
                <tr><td>Cell 4.1</td><td>Cell 4.2</td></tr>
                <tr><td>Cell 5.1</td><td>Cell 5.2</td></tr>
                <tr><td>Cell 6.1</td><td>Cell 6.2</td></tr>
                <tr><td>Cell 7.1</td><td>Cell 7.2</td></tr>
    
            </tbody>
        </table>
    </div>
    </div>
    
    table{
        border-collapse: collapse;
        table-layout: fixed;
        width: 100%;
    }
    /* Not required, just helps with alignment for this example */
    td, th{
        padding: 0;
        margin: 0;
    }
    
    tbody{
        background-color: #ddf;
    }
    
    thead {
        /* Keeps the header in place. Don't forget top: 0 */
        position: absolute;
        top: 0;
        background-color: #ddd;
    
        /* The 17px is to adjust for the scrollbar width.
         * This is a new css value that makes this pure
         * css example possible */
        width: calc(100% - 17px);
        height: 20px;
    }
    
    /* Positioning container. Required to position the
     * header since the header uses position:absolute
     * (otherwise it would position at the top of the screen) */
    #positioning-container{
        position: relative;
    }
    
    /* A container to set the scroll-bar and
     * includes padding to move the table contents
     * down below the header (padding = header height) */
    #scroll-container{
        overflow-y: auto;
        padding-top: 20px;
        height: 100px;
    }
    .header-col1{
        background-color: red;
    }
    
    /* Fixed-width header columns need a div to set their width */
    .header-col1 div{
        width: 100px;
    }
    
    /* Expandable columns need a width set on the th tag */
    .header-col2{
        width: 100%;
    }
    .col1 {
        width: 100px;
    }
    .col2{
        width: 100%;
    }
    
    table {
        overflow-x:scroll;
    }
    
    tbody {
        max-height: /*your desired max height*/
        overflow-y:scroll;
        display:block;
    }
    
    <table class="tablesorter boxlist" id="pmtable">
        <thead class="fixedheader">
            <tr class="boxheadrow">
                <th width="70px" class="header">Job Number</th>
                <th width="10px" class="header">Pri</th>
                <th width="70px" class="header">CLLI</th>
                <th width="35px" class="header">Market</th>
                <th width="35px" class="header">Job Status</th>
                <th width="65px" class="header">Technology</th>
                <th width="95px;" class="header headerSortDown">MEI</th>
                <th width="95px" class="header">TEO Writer</th>
                <th width="75px" class="header">Quote Due</th>
                <th width="100px" class="header">Engineer</th>
                <th width="75px" class="header">ML Due</th>
                <th width="75px" class="header">ML Complete</th>
                <th width="75px" class="header">SPEC Due</th>
                <th width="75px" class="header">SPEC Complete</th>
                <th width="100px" class="header">Install Supervisor</th>
                <th width="75px" class="header">MasTec OJD</th>
                <th width="75px" class="header">Install Start</th>
                <th width="30px" class="header">Install Hours</th>
                <th width="75px" class="header">Revised CRCD</th>
                <th width="75px" class="header">Latest Ship-To-Site</th>
                <th width="30px" class="header">Total Parts</th>
                <th width="30px" class="header">OEM Rcvd</th>
                <th width="30px" class="header">Minor Rcvd</th>
                <th width="30px" class="header">Total Received</th>
                <th width="30px" class="header">% On Site</th>
                <th width="60px" class="header">Actions</th>
            </tr>
        </thead>
            <tbody class="scrollable">
                <tr data-job_id="3548" data-ml_id="" class="odd">
                    <td class="c black">FL-8-RG9UP</td>
                    <td data-pri="2" class="priority c yellow">M</td>
                    <td class="c">FTLDFLOV</td>
                    <td class="c">SFL</td>
                    <td class="c">NOI</td>
                    <td class="c">TRANSPORT</td>
                    <td class="c"></td>
                    <td class="c">Chris Byrd</td>
                    <td class="c">Apr 13, 2013</td>
                    <td class="c">Kris Hall</td>
                    <td class="c">May 20, 2013</td>
                    <td class="c">May 20, 2013</td>
                    <td class="c">Jun 5, 2013</td>
                    <td class="c">Jun 7, 2013</td>
                    <td class="c">Joseph Fitz</td>
                    <td class="c">Jun 10, 2013</td>
                    <td class="c">TBD</td>
                    <td class="c">123</td>
                    <td class="c revised_crcd"><input readonly="true" name="revised_crcd" value="Jul 26, 2013" type="text" size="12" class="smInput r_crcd c hasDatepicker" id="dp1377194058616"></td>
                    <td class="c">TBD</td>
                    <td class="c">N/A</td>
                    <td class="c">N/A</td>
                    <td class="c">N/A</td>
                    <td class="c">N/A</td>
                    <td class="c">N/A</td>
                    <td class="actions"><span style="float:left;" class="ui-icon ui-icon-folder-open editJob" title="View this job" s="" details'=""></span></td>
                </tr>
                <tr data-job_id="4264" data-ml_id="2959" class="even">
                    <td class="c black">MTS13009SF</td>
                    <td data-pri="2" class="priority c yellow">M</td>
                    <td class="c">OJUSFLTL</td>
                    <td class="c">SFL</td>
                    <td class="c">NOI</td>
                    <td class="c">TRANSPORT</td>
                    <td class="c"></td>
                    <td class="c">DeMarcus Stewart</td>
                    <td class="c">May 22, 2013</td>
                    <td class="c">Ryan Alsobrook</td>
                    <td class="c">Jun 19, 2013</td>
                    <td class="c">Jun 27, 2013</td>
                    <td class="c">Jun 19, 2013</td>
                    <td class="c">Jul 4, 2013</td>
                    <td class="c">Randy Williams</td>
                    <td class="c">Jun 21, 2013</td>
                    <td class="c">TBD</td>
                    <td class="c">95</td>
                    <td class="c revised_crcd"><input readonly="true" name="revised_crcd" value="Aug 9, 2013" type="text" size="12" class="smInput r_crcd c hasDatepicker" id="dp1377194058632"></td><td class="c">TBD</td>
                    <td class="c">0</td>
                    <td class="c">0.00%</td>
                    <td class="c">0.00%</td>
                    <td class="c">0.00%</td>
                    <td class="c">0.00%</td>
                    <td class="actions"><span style="float:left;" class="ui-icon ui-icon-folder-open editJob" title="View this job" s="" details'=""></span><input style="float:left;" type="hidden" name="req_ship" class="reqShip hasDatepicker" id="dp1377194058464"><span style="float:left;" class="ui-icon ui-icon-calendar requestShip" title="Schedule this job for shipping"></span><span class="ui-icon ui-icon-info viewOrderInfo" style="float:left;" title="Show material details for this order"></span></td>
                </tr>
                .
                .
                .
                .
                <tr class="boxheadrow repeated-header">
                    <th width="70px" class="header">Job Number</th>
                    <th width="10px" class="header">Pri</th>
                    <th width="70px" class="header">CLLI</th>
                    <th width="35px" class="header">Market</th>
                    <th width="35px" class="header">Job Status</th>
                    <th width="65px" class="header">Technology</th>
                    <th width="95px;" class="header">MEI</th>
                    <th width="95px" class="header">TEO Writer</th>
                    <th width="75px" class="header">Quote Due</th>
                    <th width="100px" class="header">Engineer</th>
                    <th width="75px" class="header">ML Due</th>
                    <th width="75px" class="header">ML Complete</th>
                    <th width="75px" class="header">SPEC Due</th>
                    <th width="75px" class="header">SPEC Complete</th>
                    <th width="100px" class="header">Install Supervisor</th>
                    <th width="75px" class="header">MasTec OJD</th>
                    <th width="75px" class="header">Install Start</th>
                    <th width="30px" class="header">Install Hours</th>
                    <th width="75px" class="header">Revised CRCD</th>
                    <th width="75px" class="header">Latest Ship-To-Site</th>
                    <th width="30px" class="header">Total Parts</th>
                    <th width="30px" class="header">OEM Rcvd</th>
                    <th width="30px" class="header">Minor Rcvd</th>
                    <th width="30px" class="header">Total Received</th>
                    <th width="30px" class="header">% On Site</th>
                    <th width="60px" class="header">Actions</th>
                </tr>
    
    // Clone the original header row and add the "repeated-header" class
    var tblHeader = $('tr.boxheadrow').clone().addClass('repeated-header');
    
    // Add the cloned header with the new class every 34th row (or as you see fit)
    $('tbody tr:odd:nth-of-type(17n)').after(tblHeader);
    
    // On the 'sortStart' routine, remove all the inserted header rows
    $('#pmtable').bind('sortStart', function() {
        $('.repeated-header').remove();
        // On the 'sortEnd' routine, add back all the header row lines.
    }).bind('sortEnd', function() {
        $('tbody tr:odd:nth-of-type(17n)').after(tblHeader);
    });
    
    $('table').scrollTableBody();
    
    $(<table selector>).fxdHdrCol({
        width:     "100%",
        height:    200,
        colModal: [{width: 30, align: 'center'},
                   {width: 70, align: 'center'}, 
                   {width: 200, align: 'left'}, 
                   {width: 100, align: 'center'}, 
                   {width: 70, align: 'center'}, 
                   {width: 250, align: 'center'}
                  ]
    });
    
    var setHeaderTableWidth= function (headertableid,basetableid) {
                $("#"+headertableid).width($("#"+basetableid).width());
                $("#"+headertableid+" tr th").each(function (i) {
                    $(this).width($($("#"+basetableid+" tr:first td")[i]).width());
                });
                $("#" + headertableid + " tr td").each(function (i) {
                    $(this).width($($("#" + basetableid + " tr:first td")[i]).width());
                });
            }
    
     <table id="headertable1" class="input-cells table-striped">
            <thead>
                <tr style="background-color:darkgray;color:white;"><th>header1</th><th>header2</th><th>header3</th><th>header4</th><th>header5</th><th>header6</th><th></th></tr>
            </thead>
         </table>
        <div id="resizeToBottom" style="overflow-y:scroll;overflow-x:hidden;">
            <table id="basetable1" class="input-cells table-striped">
                <tbody >
                    <tr>
                        <td>testdata</td>
                        <td>2</td>
                        <td>3</td>
                        <td>4</span></td>
                        <td>55555555555555</td>
                        <td>test</td></tr>
                </tbody>
            </table>
        </div>
    
            setHeaderTableWidth('headertable1', 'basetable1');
            $(window).resize(function () {
                setHeaderTableWidth('headertable1', 'basetable1');
            });
    
    $.fn.sticky = function(){
        $(this).each( function(){
            var thead = $(this),
                tbody = thead.next('tbody');
    
            updateHeaderPosition();
    
            function updateHeaderPosition(){
                if(
                    thead.offset().top < $(document).scrollTop()
                    && tbody.offset().top + tbody.height() > $(document).scrollTop()
                ){
                    var tr = tbody.find('tr').last(),
                        y = tr.offset().top - thead.height() < $(document).scrollTop()
                            ? tr.offset().top - thead.height() - thead.offset().top
                            : $(document).scrollTop() - thead.offset().top;
    
                    thead.find('th').css({
                        'z-index': 100,
                        'transform': 'translateY(' + y + 'px)',
                        '-webkit-transform': 'translateY(' + y + 'px)'
                    });
                } else {
                    thead.find('th').css({
                        'transform': 'none',
                        '-webkit-transform': 'none'
                    });
                }
            }
    
            // See http://www.quirksmode.org/dom/events/scroll.html
            $(window).on('scroll', updateHeaderPosition);
        });
    }
    
    $('thead').sticky();
    
    var headerCells = tableWrap.querySelectorAll("thead td");
    for (var i = 0; i < headerCells.length; i++) {
        var headerCell = headerCells[i];
        headerCell.style.backgroundColor = "silver";
    }
    var lastSTop = tableWrap.scrollTop;
    tableWrap.addEventListener("scroll", function () {
        var stop = this.scrollTop;
        if (stop < lastSTop) {
            // Resetting the transform for the scrolling up to hide the headers
            for (var i = 0; i < headerCells.length; i++) {
                headerCells[i].style.transitionDelay = "0s";
                headerCells[i].style.transform = "";
            }
        }
        lastSTop = stop;
        var translate = "translate(0," + stop + "px)";
        for (var i = 0; i < headerCells.length; i++) {
            headerCells[i].style.transitionDelay = "0.25s";
            headerCells[i].style.transform = translate;
        }
    });
    
        var isScrolling, lastTop, lastLeft, isLeftHidden, isTopHidden;
    
        //Scroll events don't bubble https://stackoverflow.com/a/19375645/150342
        //so can't use $(document).on("scroll", ".table-container-fixed", function (e) {
        document.addEventListener('scroll', function (event) {
            var $container = $(event.target);
            if (!$container.hasClass("table-container-fixed"))
                return;    
    
            //transform needs to be applied to th for Edge and IE
            //in this example I am also fixing the leftmost column
            var $topLeftCell = $container.find('table:first > thead > tr > th:first');
            var $headerCells = $topLeftCell.siblings();
            var $columnCells = $container
               .find('table:first > tbody > tr > td:first-child, ' +
                     'table:first > tfoot > tr > td:first-child');
    
            //hide the cells while returning otherwise they show on top of the data
            if (!isLeftHidden) {
                var currentLeft = $container.scrollLeft();
                if (currentLeft < lastLeft) {
                    //scrolling left
                    isLeftHidden = true;
                    $topLeftCell.css('visibility', 'hidden');
                    $columnCells.css('visibility', 'hidden');
                }
                lastLeft = currentLeft;
            }
    
            if (!isTopHidden) {
                var currentTop = $container.scrollTop();
                if (currentTop < lastTop) {
                    //scrolling up
                    isTopHidden = true;
                    $topLeftCell.css('visibility', 'hidden');
                    $headerCells.css('visibility', 'hidden');
                }
                lastTop = currentTop;
            }
    
            // Using timeout to delay transform until user stops scrolling
            // Clear timeout while scrolling
            window.clearTimeout(isScrolling);
    
            // Set a timeout to run after scrolling ends
            isScrolling = setTimeout(function () {
                //move the table cells. 
                var x = $container.scrollLeft();
                var y = $container.scrollTop();
    
                $topLeftCell.css('transform', 'translate(' + x + 'px, ' + y + 'px)');
                $headerCells.css('transform', 'translateY(' + y + 'px)');
                $columnCells.css('transform', 'translateX(' + x + 'px)');
    
                isTopHidden = isLeftHidden = false;
                $topLeftCell.css('visibility', 'inherit');
                $headerCells.css('visibility', 'inherit');
                $columnCells.css('visibility', 'inherit');
            }, 100);
    
        }, true);
    
    .table-container-fixed{
        overflow: auto;
        height: 400px;
    }
    
    .table-container-fixed > table {
       border-collapse: separate;
       border:none;
    }
    
     .table-container-fixed > table > thead > tr > th {
            border-top: 1px solid #ddd !important;
            background-color: white;        
            z-index: 10;
            position: relative;/*to make z-index work*/
        }
    
                .table-container-fixed > table > thead > tr > th:first-child {
                    z-index: 20;
                }
    
    .table-container-fixed > table > tbody > tr > td:first-child,
    .table-container-fixed > table > tfoot > tr > td:first-child {
        background-color: white;        
        z-index: 10;
        position: relative;
    }
    
    document.getElementById('pnlGridWrap').addEventListener("scroll", function () {
      var translate = "translate(0," + this.scrollTop + "px)";
      var myElements = this.querySelectorAll("th");
      for (var i = 0; i < myElements.length; i++) {
        myElements[i].style.transform=translate;
      }
    });
    
    thead th { position: sticky; top: 0; }
    
    <html>
    <head>
        <script src="//cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js"></script>
        <script>
            function stickyTableHead (tableID) {
                var $tmain = $(tableID);
                var $tScroll = $tmain.children("thead")
                    .clone()
                    .wrapAll('<table id="tScroll" />')
                    .parent()
                    .addClass($(tableID).attr("class"))
                    .css("position", "fixed")
                    .css("top", "0")
                    .css("display", "none")
                    .prependTo("#tMain");
    
                var pos = $tmain.offset().top + $tmain.find(">thead").height();
    
    
                $(document).scroll(function () {
                    var dataScroll = $tScroll.data("scroll");
                    dataScroll = dataScroll || false;
                    if ($(this).scrollTop() >= pos) {
                        if (!dataScroll) {
                            $tScroll
                                .data("scroll", true)
                                .show()
                                .find("th").each(function () {
                                    $(this).width($tmain.find(">thead>tr>th").eq($(this).index()).width());
                                });
                        }
                    } else {
                        if (dataScroll) {
                            $tScroll
                                .data("scroll", false)
                                .hide()
                            ;
                        }
                    }
                });
            }
    
            $(document).ready(function () {
                stickyTableHead('#tMain');
            });
        </script>
    </head>
    
    <body>
        gfgfdgsfgfdgfds<br/>
        gfgfdgsfgfdgfds<br/>
        gfgfdgsfgfdgfds<br/>
        gfgfdgsfgfdgfds<br/>
        gfgfdgsfgfdgfds<br/>
        gfgfdgsfgfdgfds<br/>
    
        <table id="tMain" >
            <thead>
            <tr>
                <th>1</th> <th>2</th><th>3</th> <th>4</th><th>5</th> <th>6</th><th>7</th> <th>8</th>
    
            </tr>
            </thead>
            <tbody>
                <tr><td>11111111111111111111111111111111111111111111111111111111</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
                <tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5555555</td><td>66666666666</td><td>77777777777</td><td>8888888888888888</td></tr>
            </tbody>
        </table>
    </body>
    </html>