-
Notifications
You must be signed in to change notification settings - Fork 2
/
goheadfixed.js
46 lines (35 loc) · 1.67 KB
/
goheadfixed.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
function goheadfixed(classtable) {
if($(classtable).length) {
$(classtable).wrap('<div class="fix-inner"></div>');
$('.fix-inner').wrap('<div class="fix-outer" style="position:relative; margin:auto;"></div>');
$('.fix-outer').append('<div class="fix-head"></div>');
$('.fix-head').prepend($('.fix-inner').html());
$('.fix-head table').find('caption').remove();
$('.fix-head table').css('width','100%');
$('.fix-outer').css('width', $('.fix-inner table').outerWidth(true)+'px');
$('.fix-head').css('width', $('.fix-inner table').outerWidth(true)+'px');
$('.fix-head').css('height', $('.fix-inner table thead').height()+'px');
// If exists caption, calculte his height for then remove of total
var hcaption = 0;
if($('.fix-inner table caption').length != 0)
hcaption = parseInt($('.fix-inner table').find('caption').height()+'px');
// Table's Top
var hinner = parseInt( $('.fix-inner').offset().top );
// Let's remember that <caption> is the beginning of a <table>, it mean that his top of the caption is the top of the table
$('.fix-head').css({'position':'absolute', 'overflow':'hidden', 'top': hcaption+'px', 'left':0, 'z-index':100 });
$(window).scroll(function () {
var vscroll = $(window).scrollTop();
if(vscroll >= hinner + hcaption)
$('.fix-head').css('top',(vscroll-hinner)+'px');
else
$('.fix-head').css('top', hcaption+'px');
});
/* If the windows resize */
$(window).resize(goresize);
}
}
function goresize() {
$('.fix-head').css('width', $('.fix-inner table').outerWidth(true)+'px');
$('.fix-head').css('height', $('.fix-inner table thead').height()+'px');
hinner = parseInt( $('.fix-inner').offset().top );
}