Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Created November 2, 2012 23:24
Show Gist options
  • Save jhthorsen/4004996 to your computer and use it in GitHub Desktop.
Save jhthorsen/4004996 to your computer and use it in GitHub Desktop.
Fixed table header
(function($) {
$.fn.fixedHeader = function() {
this.each(function() {
var $table = $(this);
var $dummy = $('<table><tr></tr></table>');
var $th = $table.find('thead:first tr:first th').clone();
var $td = $table.find('tbody:first tr:first td');
var place = function() {
$dummy.css({
top: $table.offset().top,
left: $table.offset().left,
width: $table.width()
});
$th.each(function(n, e) {
$(e).width($td.eq(n).width());
});
};
$dummy
.attr('class', $table.attr('class'))
.removeClass('table-with-fixed-header')
.addClass('table-fixed-header')
.find('tr')
.append($th)
;
$('body').append($dummy);
$(window).resize(function() { place(); }).resize();
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment