Situation: A page with a table with several rows. I want to fix thead when thead reach the top of the page when scrolling, using jquery or any given scripting. I don\'t wan
(function ($) {
$.fn.fixedHeader = function () {
return this.filter('table').each(function () {
var that = this;
var $head = $('').addClass('head');
$(this).before($head);
$('th', this).each(function () {
var $el = $(this);
var $div = $('').width($el.outerWidth()).text(
$el.text());
$head.append($div);
});
$(window).on('scroll', function () {
var $that = $(that);
var w = $(window).scrollTop();
var o = $('th', that).first().offset().top;
var tableO = $that.offset().top + $that.height();
if (o < w && tableO > w) {
$head.show();
} else {
$head.hide();
}
});
});
};
})(jQuery);
You can use this jquery plugin (you need to adapt .head style to your thead style). Working example here: http://jsfiddle.net/lukasi/7K52p/1/