Skip to content

Instantly share code, notes, and snippets.

@sealucky7
Created June 28, 2017 09:21
Show Gist options
  • Save sealucky7/6b944c35fd2dc51e565646f66f27e96e to your computer and use it in GitHub Desktop.
Save sealucky7/6b944c35fd2dc51e565646f66f27e96e to your computer and use it in GitHub Desktop.
;(function($) {
$.fn.fixMe = function() {
return this.each(function() {
var $this = $(this),
$t_fixed;
function init() {
$this.wrap('<div class="container-table" />');
$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); // Function fixMe
jQuery("your table class").fixMe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment