-
Notifications
You must be signed in to change notification settings - Fork 54
/
table-fixed-header.js
67 lines (55 loc) · 1.98 KB
/
table-fixed-header.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function($) {
$.fn.fixedHeader = function (options) {
var config = {
topOffset: 38
//bgColor: 'white'
};
if (options){ $.extend(config, options); }
return this.each( function() {
var o = $(this);
var $win = $(window)
, $head = $('thead.header', o)
, isFixed = 0;
var headTop = $head.length && $head.offset().top - config.topOffset;
function processScroll() {
if (!o.is(':visible')) return;
if ($('thead.header-copy').size()) {
$('thead.header-copy').width($head.width());
var i, scrollTop = $win.scrollTop();
}
var t = $head.length && $head.offset().top - config.topOffset;
if (!isFixed && headTop != t) { headTop = t; }
if (scrollTop >= headTop && !isFixed) {
isFixed = 1;
} else if (scrollTop <= headTop && isFixed) {
isFixed = 0;
}
isFixed ? $('thead.header-copy', o).show().offset({ left: $head.offset().left })
: $('thead.header-copy', o).hide();
// NG: dislocate while iframe page resized. fixed by jeffen@pactera 2015/7/8
headerCopyRectify();
}
// set a broken bone when header copy dislocated
function headerCopyRectify() {
o.find('thead.header > tr > th').each(function (i, h) {
var w = $(h).width();
o.find('thead.header-copy> tr > th:eq('+i+')').width(w)
});
}
$win.on('scroll', processScroll);
// NG: dislocate while body resized. fixed by jeffen@pactera 2015/7/9
$win.on('resize', processScroll);
// hack sad times - holdover until rewrite for 2.1
$head.on('click', function () {
if (!isFixed) setTimeout(function () { $win.scrollTop($win.scrollTop() - 47) }, 10);
})
$head.clone(true).removeClass('header').addClass('header-copy header-fixed').css({'position': 'fixed', 'top': config['topOffset']}).appendTo(o);
o.find('thead.header-copy').width($head.width());
headerCopyRectify();
$head.css({ margin:'0 auto',
width: o.width(),
'background-color':config.bgColor });
processScroll();
});
};
})(jQuery);