Project

General

Profile

1
/**
2
 * Stupid Fixed Header 1.0.1 - jQuery plugins to create a fixed headers
3
 * 
4
 * Require: jQuery 1.2.6
5
 * Author: Jacky See
6
 * Blog: http://jacky.seezone.net
7
 * email:  jackysee at gmail dot com
8
 * Dual licensed under the MIT and GPL licenses:
9
 *   http://www.opensource.org/licenses/mit-license.php
10
 *   http://www.gnu.org/licenses/gpl.html
11
*/
12

    
13
(function($){
14
	
15
	/* created fixed headers , require jquery dimenions plugins*/
16
	$.fn.fixedHeader = function(o){
17
		var s = {adjustWidth: $.fixedHeader.calcWidth};
18
		if(o) $.extend(s,o);
19
		
20
		return this.each(function(){
21
			var table = $(this); //table
22
			var tId = this.id;
23
			
24
			var scrollBarWidth = $.fixedHeader.getScrollBarWidth();
25
			var IE6 = $.browser.msie && $.browser.version == '6.0';
26
			
27
			//wrap a body container
28
			var bodyContainer = table.wrap('<div></div>').parent()
29
				.attr('id', tId + "_body_container")
30
				.css({
31
					width: s.width,
32
					height: s.height,
33
					overflow: 'auto'
34
				});
35
			
36
			//Wrap with an overall container
37
			var tableContainer = bodyContainer.wrap('<div></div>').parent()
38
				.attr('id', tId + '_table_container')
39
				.css('position','relative');
40

    
41
			//clone the header
42
			var headerContainer = $(document.createElement('div'))
43
				.attr('id', tId + '_header_container')
44
				.css({
45
					width:  bodyContainer.innerWidth() - scrollBarWidth,
46
					height: table.find('thead').outerHeight(), 
47
					overflow: 'hidden',
48
					top: 0, left:0
49
				})
50
				.prependTo(tableContainer);
51
			
52
			var headerTable = table.clone(true)
53
				.find('tbody').remove().end()
54
				.attr('id',tId + "_header")
55
				.addClass(s.tableClass || table[0].className)
56
				.css({
57
					//width: $.browser.msie? table.outerWidth():table.width(), 
58
					'table-layout':'fixed',
59
					position:'absolute',
60
					top:0, left:0
61
				})
62
				.append(table.find('thead').clone(true))
63
				.appendTo(headerContainer);
64
			
65
			//sync header width
66
			var headThs = headerTable.find('th');
67
			table.find('th').each(function(i){
68
				headThs.eq(i).css('width', s.adjustWidth(this));
69
			})
70
			
71
			//sync scroll
72
			var selects = IE6? table.find("select"): null;
73
			bodyContainer.scroll(function(){
74
				if(IE6 && selects.size()>0){
75
					selects.each(function(i){
76
						this.style.visibility =
77
							($(this).offset().top - bodyContainer.offset().top) <= table.find("thead").outerHeight() + 10
78
							? 'hidden':'visible';
79
					});
80
				}
81
				headerTable.css({
82
					left: '-' + $(this).scrollLeft() + 'px'
83
				});
84
			})
85
			
86
			//Move it down
87
			headerContainer.css({
88
				'position': 'absolute',
89
				'top': 0
90
			});
91
		});
92
	}
93
	
94
	$.fixedHeader = {
95
		calcWidth: function(th){
96
			var w = $(th).width();
97
			var paddingLeft = $.fixedHeader.getComputedStyleInPx(th,'paddingLeft');
98
			var paddingRight = $.fixedHeader.getComputedStyleInPx(th,'paddingRight');
99
			var borderWidth = $.fixedHeader.getComputedStyleInPx(th,'borderRightWidth');			
100
			if($.browser.msie) w = w+borderWidth;
101
			if($.browser.opera) w = w+borderWidth;
102
			if($.browser.safari) w = w+paddingLeft+paddingRight+borderWidth*2;
103
			if($.browser.mozilla && parseFloat($.browser.version) <= 1.8) w=w+borderWidth; //FF2 still got a border-left missing problem, this is the best I can do.
104
			return w;
105
		},
106
		getComputedStyleInPx: function(elem,style){
107
			var computedStyle = (typeof elem.currentStyle != 'undefined')
108
				?elem.currentStyle
109
				:document.defaultView.getComputedStyle(elem, null);
110
			var val = computedStyle[style];
111
			val = val? parseInt(val.replace("px","")):0;
112
			return (!val || val == 'NaN')?0:val;
113
		},
114
		getScrollBarWidth: function() { //calculate or get from global the scroll bar width
115
			if(!$.fixedHeader.scrollBarWidth){ 
116
				var inner = $(document.createElement('p')).css({width:'100%',height:'100%'});
117
				var outer = $(document.createElement('div'))
118
					.css({
119
						position:'absolute',
120
						top: '0px',
121
						left: '0px',
122
						visibility: 'hidden',
123
						width: '200px',
124
						height: '150px',
125
						overflow: 'hidden'
126
					})
127
					.append(inner)
128
					.appendTo(document.body);
129
				
130
				var w1 = inner[0].offsetWidth;
131
				outer[0].style.overflow = 'scroll';
132
				var w2 = inner[0].offsetWidth;
133
				if (w1 == w2) w2 = outer[0].clientWidth;
134
				document.body.removeChild (outer[0]);
135
				$.fixedHeader.scrollBarWidth = (w1 - w2);
136
			}
137
			return $.fixedHeader.scrollBarWidth;
138
		}
139
	}
140
	
141
})(jQuery);
(2-2/6)