【问题标题】:Table Header Fixed beneath Fixed Page Header表头固定在固定页眉下方
【发布时间】:2018-06-18 23:03:17
【问题描述】:

表头是固定的,但滚动后在页面顶部。它完全按照我想要的方式工作,除了thead 固定在错误的位置。

表格有一个 overflow-x:auto 并且 TD 正在使用 white-space:nowrap 所以表格扩展以处理内容。

我需要将其固定在距页眉顶部或右下方 140 像素处。 我不知道如何抵消这个...它关闭,但需要考虑到溢出...

这里是 JSFIDDLE - https://jsfiddle.net/rbla/1Ljuycbe/1/

请查看 FIRST 表...问题出在 OVERFLOW-X:AUTO - 我也需要克隆表来反映这一点...

JQUERY

 ;(function($) {
   $.fn.fixMe = function() {
  return this.each(function() {
     var $this = $(this),
        $t_fixed;
     function init() {
        $this.wrap('<div class="container" />');
        $t_fixed = $this.clone();
        $t_fixed.find("tbody").remove().end().addClass("fixed").insertBefore($this);
        resizeFixed();
     }
     function resizeFixed() {
        $t_fixed.find("th").each(function(index) {
           $(this).css("width",$this.find("th").eq(index).outerWidth()+"px");
        });
     }
     function scrollFixed() {
        var offset = $(this).scrollTop(),
        tableOffsetTop = $this.offset().top,
        tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
        if(offset < tableOffsetTop || offset > tableOffsetBottom)
           $t_fixed.hide();
        else if(offset >= tableOffsetTop && offset <= tableOffsetBottom && $t_fixed.is(":hidden"))
           $t_fixed.show();
     }
     $(window).resize(resizeFixed);
     $(window).scroll(scrollFixed);
     init();
  });
     };})(jQuery);


$(document).ready(function(){
   $("table").fixMe();
   $(".up").click(function() {
       $('html, body').animate({
       scrollTop: 0
   }, 2000);
   });
});

CSS

 h1, h2 {
       text-align: center;
       text-transform: uppercase;
      }

    .container {
       width: 90%;
       margin: auto;
       overflow-x:auto; /* MUST KEEP */
      }

     table {
       border-collapse:collapse;
       width:100%;
      }

     .blue {
       border:2px solid #1ABC9C;
      }

     .blue thead {
       background:#1ABC9C;
     }

     .purple{
       border:2px solid #9B59B6;
     }

.purple thead{
  background:#9B59B6;
}

thead {
  color:white;
}

th,td {
  text-align:center;
  padding:5px 0;
  white-space: nowrap; /* MUST KEEP */
}

tbody tr:nth-child(even) {
  background:#ECF0F1;
}

tbody tr:hover {
background:#BDC3C7;
  color:#FFFFFF;
}

.fixed {
  top:0;
  position:fixed;
  width:auto;
  display:none;
  border:none;
}

.scrollMore {
  margin-top:10px;
}

.up {
  cursor:pointer;
}

.header {
    font: 13px Arial, Helvetica, sans-serif;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 1000;
    height: 140px;
    border: 1px solid #000;
    }

 #pure {
   margin-top:200px;
 }

;
(function($) {
  $.fn.fixMe = function() {
    return this.each(function() {
      var $this = $(this),
        $t_fixed,
        $header_height = $('header').height();

      function init() {
        $this.wrap('<div class="container" />');
        $t_fixed = $this.clone();
        $t_fixed.find("tbody").remove().end().addClass("fixed").css({
          top: $header_height + "px"
        }).insertBefore($this);
        resizeFixed();
      }

      function resizeFixed() {
        $t_fixed.find("th").each(function(index) {
          $(this).css("width", $this.find("th").eq(index).outerWidth() + "px");
        });
      }

      function scrollFixed() {
        var offset = $(this).scrollTop(),
          tableOffsetTop = $this.offset().top,
          tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
        if (offset + $header_height < tableOffsetTop || offset + $header_height > tableOffsetBottom)
          $t_fixed.hide();
        else if (offset + $header_height >= tableOffsetTop && offset + $header_height <= tableOffsetBottom && $t_fixed.is(":hidden"))
          $t_fixed.show();
      }
      $(window).resize(resizeFixed);
      $(window).scroll(scrollFixed);
      init();
    });
  };
})(jQuery);

$(document).ready(function() {
  $("table").fixMe();
  $(".up").click(function() {
    $('html, body').animate({
      scrollTop: 0
    }, 2000);
  });
});
h1,
h2 {
  text-align: center;
  text-transform: uppercase;
}

.container {
  width: 90%;
  margin: auto;
  overflow-x: auto;
  /* MUST KEEP */
}

table {
  border-collapse: collapse;
  width: 100%;
}

.blue {
  border: 2px solid #1ABC9C;
}

.blue thead {
  background: #1ABC9C;
}

.purple {
  border: 2px solid #9B59B6;
}

.purple thead {
  background: #9B59B6;
}

thead {
  color: white;
}

th,
td {
  text-align: center;
  padding: 5px 0;
  white-space: nowrap;
  /* MUST KEEP */
}

tbody tr:nth-child(even) {
  background: #ECF0F1;
}

tbody tr:hover {
  background: #BDC3C7;
  color: #FFFFFF;
}

.fixed {
  top: 0px;
  position: fixed;
  width: auto;
  display: none;
  border-top: none;
  border-bottom: none;
}

.scrollMore {
  margin-top: 10px;
}

.up {
  cursor: pointer;
}

.header {
  font: 13px Arial, Helvetica, sans-serif;
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 1000;
  height: 140px;
  border: 1px solid #000;
}

#pure {
  margin-top: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="header">Fixed Header - 140PX</header>

<div id="pure">

  <h1>Table Fixed Header</h1>

  <h2>At Bottom of Fixed Header</h2>

  <h2>&darr; SCROLL &darr;</h2>



  <div class="container">

    <table class="blue">
      <thead>
        <tr>
          <th>Colonne 1</th>
          <th>Colonne 2</th>
          <th>Colonne 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
          <td>Mais Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
          <td>Allo Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
      </tbody>
    </table>
  </div>

  <h2 class="scrollMore">&darr; SCROLL MORE &darr;</h2>

  <div class="container">
    <table class="purple">
      <thead>
        <tr>
          <th>Colonne 1</th>
          <th>Colonne 2</th>
          <th>Colonne 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
        <tr>
          <td>Non</td>
          <td>Mais</td>
          <td>Allo !</td>
        </tr>
      </tbody>
    </table>
  </div>

  <h2 class="up scrollMore">&uarr; UP &uarr;</h2>

</div>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    这是对上述 Munim Munna 接受的answer 的建议改进。


    我想我已经确定了,但如果由于某种原因这与您想要的不一致,请随时告诉我。浮动表格标题不保留其下方静态标题的确切位置,但宽度相同,并且在表格上滚动看起来很流畅。在 .header 的 CSS 中,我添加了它以使其更美观。

    width:97%;
    margin-left:1%;
    margin-right:2%;
    

    但有一件事引起了我的注意,我在 resizeFixed() 中注意到了这一点:

    $table_header_wrap.width($container.width() + 10);
    

    我相信这就是导致长表格标题(.blue.purple)“挂在”表格右侧的原因。我改成这个了。

    $table_header_wrap.width($container.width() - 1);
    

    现在浮动表格标题与表格主体对齐。

    ;
    (function($) {
    $.fn.fixMe = function() {
        return this.each(function() {
            var $this = $(this),
            $t_fixed, $table_wrap, $table_header_wrap, $container,
            $header_height = $('header').height();
    
            function init() {
                $container = $this.parent();
                $table_header_wrap = $('<div>').addClass('table_header_wrap').insertAfter($this);
                $table_wrap = $('<div>').addClass('table_wrap').insertAfter($table_header_wrap).append($this);
                $t_fixed = $this.clone().find("tbody").remove().end().hide().appendTo($table_header_wrap);
                $table_header_wrap.css({
                    top: $header_height + "px"
                }).on('scroll', header_wrap_scroll);
                resizeFixed();
            }
    
            function resizeFixed() {
                $table_header_wrap.width($container.width() - 1);
                $t_fixed.width($this.width() + 2);
                $t_fixed.find("th").each(function(index) {
                    $(this).css("width", $this.find("th").eq(index).outerWidth() + "px");
                });
            }
    
            function scrollFixed() {
                var offset = $(this).scrollTop(),
                    tableOffsetTop = $this.offset().top,
                    tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
                if (offset + $header_height < tableOffsetTop || offset + $header_height > tableOffsetBottom)
                    $t_fixed.hide();
                else if (offset + $header_height >= tableOffsetTop && offset + $header_height <= tableOffsetBottom && $t_fixed.is(":hidden"))
                    $t_fixed.show();
            }
    
            function header_wrap_scroll() {
                $table_wrap.scrollLeft($table_header_wrap.scrollLeft());
            }
            $(window).resize(resizeFixed);
            $(window).scroll(scrollFixed);
            init();
        });
    };
    })(jQuery);
    
    $(document).ready(function() {
        $("table").fixMe();
        $(".up").click(function() {
            $('html, body').animate({
                scrollTop: 0
            }, 2000);
        });
    });
    h1, h2 {
        text-align: center;
        text-transform: uppercase;
    }
    
    .container {
        width: 90%;
        margin: auto;
        max-width: 90%;
    }
    
    table {
        width:100%;
        border-collapse: collapse;
        max-width:100%;
    }
    
    table thead {
        width:100%;
        overflow-x:hidden;
        border-collapse:collapse;
    }
    
    .fixed {
        border-collapse:collapse;
        border:inherit;
    }
    
    .blue {
        border: 2px solid #1ABC9C;
    }
    
    .blue thead {
        background: #1ABC9C;
    }
    
    .purple {
        border: 2px solid #9B59B6;
    }
    
    .purple thead {
        background: #9B59B6;
    }
    
    thead {
        color: white;
    }
    
    th, td {
        text-align: center;
        padding: 5px 0;
        white-space: nowrap;
        /* MUST KEEP */
    }
    
    tbody tr:nth-child(even) {
        background: #ECF0F1;
    }
    
    tbody tr:hover {
        background: #BDC3C7;
        color: #FFFFFF;
    }
    
    .table_wrap {
        overflow-x: hidden;
    }
    
    .table_header_wrap {
        position: fixed;
        overflow-x: auto;
    }
    
    .scrollMore {
        margin-top: 10px;
    }
    
    .up {
        cursor: pointer;
    }
    
    .header {
        font: 13px Arial, Helvetica, sans-serif;
        position: fixed;
        top: 0;
        width: 97%;
        margin-left:1%;
        margin-right:2%;
        z-index: 1000;
        height: 140px;
        border: 1px solid #000;
    }
    
    #pure {
        margin-top: 200px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <header class="header">Fixed Header - 140PX</header>
    <div id="pure">
        <h1>Table Fixed Header</h1>
        <h2>At Bottom of Fixed Header</h2>
        <h2>&darr; SCROLL &darr;</h2>
        <div class="container">
    
        <table class="blue">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Mais Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Allo Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
        </div>
    
        <h2 class="scrollMore">&darr; SCROLL MORE &darr;</h2>
    
        <div class="container">
        <table class="purple">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
        </div>
        
        <h2 class="up scrollMore">&uarr; UP &uarr;</h2>
        
    </div>

    希望对你有所帮助。

    【讨论】:

    • 这只是对我的回答的轻微改进。这应该作为对我的回答的评论发布。不是一个不同的答案。
    【解决方案2】:

    这解决了滚动标题的标题问题。

    (function($) {
      $.fn.fixMe = function() {
        return this.each(function() {
          var $this = $(this),
            $t_fixed, $table_wrap, $table_header_wrap, $container,
            $header_height = $('header').height();
    
          function init() {
            $container = $this.parent();
            $table_header_wrap = $('<div>').addClass('table_header_wrap').insertAfter($this);
            $table_wrap = $('<div>').addClass('table_wrap').insertAfter($table_header_wrap).append($this);
            $t_fixed = $this.clone().find("tbody").remove().end().hide().appendTo($table_header_wrap);
            $table_header_wrap.css({
              top: $header_height + "px"
            }).on('scroll', header_wrap_scroll);
            resizeFixed();
          }
    
          function resizeFixed() {
            $table_header_wrap.width($container.width() + 10);
            //$table_wrap.width($container.width());
            $t_fixed.width($this.width() + 2);
            $t_fixed.find("th").each(function(index) {
              $(this).css("width", $this.find("th").eq(index).outerWidth() + "px");
            });
          }
    
          function scrollFixed() {
            var offset = $(this).scrollTop(),
              tableOffsetTop = $this.offset().top,
              tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
            if (offset + $header_height < tableOffsetTop || offset + $header_height > tableOffsetBottom)
              $t_fixed.hide();
            else if (offset + $header_height >= tableOffsetTop && offset + $header_height <= tableOffsetBottom && $t_fixed.is(":hidden"))
              $t_fixed.show();
          }
    
          function header_wrap_scroll() {
            $table_wrap.scrollLeft($table_header_wrap.scrollLeft());
          }
          $(window).resize(resizeFixed);
          $(window).scroll(scrollFixed);
          init();
        });
      };
    })(jQuery);
    
    $(document).ready(function() {
      $("table").fixMe();
      $(".up").click(function() {
        $('html, body').animate({
          scrollTop: 0
        }, 2000);
      });
    });
    h1,
    h2 {
      text-align: center;
      text-transform: uppercase;
    }
    
    .container {
      width: 90%;
      margin: auto;
    }
    
    table {
      border-collapse: collapse;
      width: 100%;
    }
    
    .blue {
      border: 2px solid #1ABC9C;
    }
    
    .blue thead {
      background: #1ABC9C;
    }
    
    .purple {
      border: 2px solid #9B59B6;
    }
    
    .purple thead {
      background: #9B59B6;
    }
    
    thead {
      color: white;
    }
    
    th,
    td {
      text-align: center;
      padding: 5px 0;
      white-space: nowrap;
      /* MUST KEEP */
    }
    
    tbody tr:nth-child(even) {
      background: #ECF0F1;
    }
    
    tbody tr:hover {
      background: #BDC3C7;
      color: #FFFFFF;
    }
    
    .table_wrap {
      overflow-x: hidden;
    }
    
    .table_header_wrap {
      position: fixed;
      overflow-x: auto;
    }
    
    .scrollMore {
      margin-top: 10px;
    }
    
    .up {
      cursor: pointer;
    }
    
    .header {
      font: 13px Arial, Helvetica, sans-serif;
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 1000;
      height: 140px;
      border: 1px solid #000;
    }
    
    #pure {
      margin-top: 200px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <header class="header">Fixed Header - 140PX</header>
    <div id="pure">
      <h1>Table Fixed Header</h1>
      <h2>At Bottom of Fixed Header</h2>
      <h2>&darr; SCROLL &darr;</h2>
      <div class="container">
    
        <table class="blue">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Mais Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Allo Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
      </div>
    
      <h2 class="scrollMore">&darr; SCROLL MORE &darr;</h2>
    
      <div class="container">
        <table class="purple">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
      </div>
    
      <h2 class="up scrollMore">&uarr; UP &uarr;</h2>
    
    </div>

    更新

    这是根据 OP 需求在表格底部带有滚动条的修复。

    (function($) {
      $.fn.fixMe = function() {
        return this.each(function() {
          var $this = $(this),
            $t_fixed, $table_wrap, $table_header_wrap, $container,
            $header_height = $('header').height();
    
          function init() {
            $container = $this.parent();
            $table_header_wrap = $('<div>').addClass('table_header_wrap').insertAfter($this);
            $table_wrap = $('<div>').addClass('table_wrap').insertAfter($table_header_wrap).append($this).on('scroll', table_wrap_scroll);
            $t_fixed = $this.clone().find("tbody").remove().end().hide().appendTo($table_header_wrap);
            $table_header_wrap.css({
              top: $header_height + "px"
            });
            resizeFixed();
          }
    
          function resizeFixed() {
            $table_header_wrap.width($container.width());
            //$table_wrap.width($container.width());
            $t_fixed.width($this.width() + 2);
            $t_fixed.find("th").each(function(index) {
              $(this).css("width", $this.find("th").eq(index).outerWidth() + "px");
            });
          }
    
          function scrollFixed() {
            var offset = $(this).scrollTop(),
              tableOffsetTop = $this.offset().top,
              tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
            if (offset + $header_height < tableOffsetTop || offset + $header_height > tableOffsetBottom)
              $t_fixed.hide();
            else if (offset + $header_height >= tableOffsetTop && offset + $header_height <= tableOffsetBottom && $t_fixed.is(":hidden"))
              $t_fixed.show();
          }
    
          function table_wrap_scroll() {
            $table_header_wrap.scrollLeft($table_wrap.scrollLeft());
          }
          $(window).resize(resizeFixed);
          $(window).scroll(scrollFixed);
          init();
        });
      };
    })(jQuery);
    
    $(document).ready(function() {
      $("table").fixMe();
      $(".up").click(function() {
        $('html, body').animate({
          scrollTop: 0
        }, 2000);
      });
    });
    h1,
    h2 {
      text-align: center;
      text-transform: uppercase;
    }
    
    .container {
      width: 90%;
      margin: auto;
    }
    
    table {
      border-collapse: collapse;
      width: 100%;
    }
    
    .blue {
      border: 2px solid #1ABC9C;
    }
    
    .blue thead {
      background: #1ABC9C;
    }
    
    .purple {
      border: 2px solid #9B59B6;
    }
    
    .purple thead {
      background: #9B59B6;
    }
    
    thead {
      color: white;
    }
    
    th,
    td {
      text-align: center;
      padding: 5px 0;
      white-space: nowrap;
      /* MUST KEEP */
    }
    
    tbody tr:nth-child(even) {
      background: #ECF0F1;
    }
    
    tbody tr:hover {
      background: #BDC3C7;
      color: #FFFFFF;
    }
    
    .table_wrap {
      overflow-x: auto;
    }
    
    .table_header_wrap {
      position: fixed;
      overflow-x: hidden;
    }
    
    .scrollMore {
      margin-top: 10px;
    }
    
    .up {
      cursor: pointer;
    }
    
    .header {
      font: 13px Arial, Helvetica, sans-serif;
      position: fixed;
      top: 0;
      width: 100%;
      z-index: 1000;
      height: 140px;
      border: 1px solid #000;
    }
    
    #pure {
      margin-top: 200px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <header class="header">Fixed Header - 140PX</header>
    <div id="pure">
      <h1>Table Fixed Header</h1>
      <h2>At Bottom of Fixed Header</h2>
      <h2>&darr; SCROLL &darr;</h2>
      <div class="container">
    
        <table class="blue">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Mais Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
              <td>Allo Non asdf asdf asdfsad fsdf asdf sadfsad sdf adf sad sadfasd fsf</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
      </div>
    
      <h2 class="scrollMore">&darr; SCROLL MORE &darr;</h2>
    
      <div class="container">
        <table class="purple">
          <thead>
            <tr>
              <th>Colonne 1</th>
              <th>Colonne 2</th>
              <th>Colonne 3</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
            <tr>
              <td>Non</td>
              <td>Mais</td>
              <td>Allo !</td>
            </tr>
          </tbody>
        </table>
      </div>
    
      <h2 class="up scrollMore">&uarr; UP &uarr;</h2>
    
    </div>

    【讨论】:

    • 这实际上并没有解决问题 - 当你以全浏览器宽度打开它时 - 看看......标题被砍掉了......
    • 尝试调整这个大小,你会看到问题。
    • @Ronald,如果你调整屏幕大小,它现在对你有用吗?
    • 调整大小时,能否让水平滚动条出现在表格底部?它的关闭...
    • 是的。起初这是我的解决方案。但在这种情况下,您需要滚动到表格底部才能左右滚动。想想吧。
    【解决方案3】:

    更新

    我已经修改了你的 js 来做你想做的事情working fiddle

    (function($) {
       $.fn.fixMe = function() {
          return this.each(function() {
             var $this = $(this),
                $t_fixed,
                $header_height = $('header').height();
             function init() {
                $this.wrap('<div class="container" />');
                $t_fixed = $this.clone();
                // width matching added
                $t_fixed.find("tbody").remove().end().addClass("fixed").css({top: $header_height + "px", "width":$this.width()+"px"}).insertBefore($this);
                //scroll attachment between elements
                $this.parent().on('scroll',function(e){
                    $t_fixed.css("margin-left",-$this.parent().scrollLeft()+"px");
                });
                //resizing fix
                var table = $this;
                $(window).on('resize',function(){
                    $t_fixed.css('width',table.width()+"px");
                });
                resizeFixed();
             }
             function resizeFixed() {
                $t_fixed.find("th").each(function(index) {
                   $(this).css("width",$this.find("th").eq(index).outerWidth()+"px");
                });
             }
             function scrollFixed() {
                var offset = $(this).scrollTop(),
                tableOffsetTop = $this.offset().top,
                tableOffsetBottom = tableOffsetTop + $this.height() - $this.find("thead").height();
                if(offset + $header_height < tableOffsetTop || offset + $header_height > tableOffsetBottom)
                   $t_fixed.hide();
                else if(offset + $header_height >= tableOffsetTop && offset + $header_height <= tableOffsetBottom && $t_fixed.is(":hidden"))
                   $t_fixed.show();
             }
             $(window).resize(resizeFixed);
             $(window).scroll(scrollFixed);
             init();
          });
       };
    })(jQuery);
    
    $(document).ready(function(){
       $("table").fixMe();
       $(".up").click(function() {
          $('html, body').animate({
          scrollTop: 0
       }, 2000);
     });
    });
    

    【讨论】:

    • 调整大小时标题不匹配
    【解决方案4】:

    我已更新您的 JSFiddle 以添加这些功能。 https://jsfiddle.net/g7wgp7gj/1/

    解决方案是在计算中查找并利用表头的高度来确定何时显示浮动表头。此标题高度也将用于动态设置浮动表格标题的“顶部”CSS 属性,以便将其固定在标题下方。

    position: fixed;
    top: <header height>px;
    

    【讨论】:

    • 我实际上在调整表格大小时遇到​​了克隆表格单元格的宽度问题
    • 在哪个浏览器中?
    • 所以我在 Chrome 中工作。我将表格设置为 98% 宽度,并且单元格也设置了百分比宽度。问题是当我在单元格中有内容时 - 单元格可能会扩展(我正在使用 css white-space: nowrap ) - 但是当克隆发生时,它只是标题行,所以不会发生扩展,因此真正的标题不匹配加上克隆的标头-它们是偏移量
    • 所以原始标题超出了浏览器宽度,因为它在 echo "
      ";但克隆留在视口中......我想知道这是否是我的问题。
    • 所以我认为问题出在表格的内容 TD 上。当我使用这个 $t_fixed.addClass("fixed").removeAttr("id").css({top: $header_height + "px"}).insertBefore($this);而不是这个 $t_fixed.find("tbody").remove().end().addClass("fixed").removeAttr("id").css({top: $header_height + "px"}).insertBefore ($这个); ---> 标题工作正常(内容已克隆)但 TH 是正确的......所以我认为问题与 TD 在内容溢出时如何拉伸有关......
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签