-
Notifications
You must be signed in to change notification settings - Fork 16
/
stickyheader.jquery.js
48 lines (42 loc) · 1.76 KB
/
stickyheader.jquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
jQuery(document).ready(function ($) {
var tables = $('table.stickyHeader');
tables.each(function(i){
var table = tables[i];
var tableClone = $(table).clone(true).empty().removeClass('stickyHeader');
var theadClone = $(table).find('thead').clone(true);
var stickyHeader = $('<div></div>').addClass('stickyHeader hide').attr('aria-hidden', 'true');
stickyHeader.append(tableClone).find('table').append(theadClone);
$(table).after(stickyHeader);
var tableHeight = $(table).height();
var tableWidth = $(table).width() + Number($(table).css('padding-left').replace(/px/ig,"")) + Number($(table).css('padding-right').replace(/px/ig,"")) + Number($(table).css('border-left-width').replace(/px/ig,"")) + Number($(table).css('border-right-width').replace(/px/ig,""));
var headerCells = $(table).find('thead th');
var headerCellHeight = $(headerCells[0]).height();
var no_fixed_support = false;
if (stickyHeader.css('position') == "absolute") {
no_fixed_support = true;
}
var stickyHeaderCells = stickyHeader.find('th');
stickyHeader.css('width', tableWidth);
var cellWidths = [];
for (var i = 0, l = headerCells.length; i < l; i++) {
cellWidths[i] = $(headerCells[i]).width();
}
for (var i = 0, l = headerCells.length; i < l; i++) {
$(stickyHeaderCells[i]).css('width', cellWidths[i]);
}
var cutoffTop = $(table).offset().top;
var cutoffBottom = tableHeight + cutoffTop - headerCellHeight;
$(window).scroll(function() {
var currentPosition = $(window).scrollTop();
if (currentPosition > cutoffTop && currentPosition < cutoffBottom) {
stickyHeader.removeClass('hide');
if (no_fixed_support) {
stickyHeader.css('top', currentPosition + 'px');
}
}
else {
stickyHeader.addClass('hide');
}
});
});
});