Created
June 28, 2017 09:21
-
-
Save sealucky7/6b944c35fd2dc51e565646f66f27e96e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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