Skip to content

Instantly share code, notes, and snippets.

@jimschubert
Created June 26, 2011 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimschubert/1047086 to your computer and use it in GitHub Desktop.
Save jimschubert/1047086 to your computer and use it in GitHub Desktop.
simple fixed table header jquery plugin
Copyright (c) 2011 Jim Schubert
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
<script type="text/javascript">
(function($) {
$.fn.fixedHeader = function(options) {
var settings = {
selector: 'thead:first',
cssClass: 'fixed',
fixTo: 0
};
var _fixHeader = function(obj) {
var header = $(obj.selector, obj.elem);
if(header) {
var parent = header.parents('table:first') || header.parent();
(parent && parent.css({ borderCollapse: 'collapse'}) );
var data = header.data('fixedHeader') || header.data('fixedHeader', {
top: header.offset().top,
width: parent.find('tr:eq(1)').width(),
cells: parent.find('tr:eq(1) > td'),
processed: false
});
var top = data.top - $(document).scrollTop();
if( top < 0 ) {
header.addClass(obj.css);
if(!data.processed){
header.width(data.width);
for(var i = 0; i<data.cells.length;i++) {
$('th:eq('+i+')', header).width($(data.cells[i]).width());
}
}
} else {
header.removeClass(obj.css);
}
}
};
return this.each(function() {
var self = this;
if("object" === typeof options) {
$.extend(settings, options);
}
if($(self).parents('table:first')){
$(window).bind('scroll.fixedHeader', function() {
_fixHeader({
elem: self,
selector: settings.selector,
css: settings.cssClass,
top: settings.fixTo
});
});
}
});
};
})(jQuery);
$(function() {
var table = $('#example');
for(var i = 1; i < 50; i++) {
var row = $('<tr><td>first_'+i+'</td><td>second_'+i+'</td><td>third_'+i+'</td><td>fourth_'+i+'</td></tr>');
$('tbody', table).append(row);
}
$('thead:not(thead:eq(0))').remove();
table.fixedHeader();
});
</script>
<style type="text/css">
.fixed { position:fixed; background:white; border-bottom:1px dotted black;}
</style>
<table id="example">
<thead>
<tr>
<th>First</th>
<th>Second</th>
<th>Third</th>
<th>Fourth</th>
</tr>
<thead>
<tbody></tbody>
</table>
<div id="scrollTop">
</div>
(function($) {
$.fn.fixedHeader = function(options) {
var settings = {
selector: 'thead:first',
cssClass: 'fixed',
fixTo: 0
};
var _fixHeader = function(obj) {
var header = $(obj.selector, obj.elem);
if(header) {
var parent = header.parents('table:first') || header.parent();
(parent && parent.css({ borderCollapse: 'collapse'}) );
var data = header.data('fixedHeader') || header.data('fixedHeader', {
top: header.offset().top,
width: parent.find('tr:eq(1)').width(),
cells: parent.find('tr:eq(1) > td'),
processed: false
});
var top = data.top - $(document).scrollTop();
if( top < 0 ) {
header.addClass(obj.css);
if(!data.processed){
header.width(data.width);
for(var i = 0; i<data.cells.length;i++) {
$('th:eq('+i+')', header).width($(data.cells[i]).width());
}
}
} else {
header.removeClass(obj.css);
}
}
};
return this.each(function() {
var self = this;
if("object" === typeof options) {
$.extend(settings, options);
}
if($(self).parents('table:first')){
$(window).bind('scroll.fixedHeader', function() {
_fixHeader({
elem: self,
selector: settings.selector,
css: settings.cssClass,
top: settings.fixTo
});
});
}
});
};
})(jQuery);
@0x3333
Copy link

0x3333 commented Feb 20, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment