﻿/* Copyright (c) 2009 Mustafa OZCAN (http://www.mustafaozcan.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Version: 1.0.2
 * Requires: jquery.1.3+
 */
jQuery.fn.fixedtableheader = function(options) { var settings = jQuery.extend({ headerrowsize: 1, highlightrow: false, highlightclass: "highlight" }, options); this.each(function(i) { var jQuerytbl = jQuery(this); var jQuerytblhfixed = jQuerytbl.find("tr:lt(" + settings.headerrowsize + ")"); var headerelement = "th"; if (jQuerytblhfixed.find(headerelement).length == 0) headerelement = "td"; if (jQuerytblhfixed.find(headerelement).length > 0) { jQuerytblhfixed.find(headerelement).each(function() { jQuery(this).css("width", jQuery(this).width()); }); var jQueryclonedTable = jQuerytbl.clone().empty(); var tblwidth = GetTblWidth(jQuerytbl); jQueryclonedTable.attr("id", "fixedtableheader" + i).css({ "background": "#ccc", "opacity": ".85", "margin": "0px", "position": "fixed", "top": "0", "left": jQuerytbl.offset().left }).append(jQuerytblhfixed.clone()).width(tblwidth).hide().appendTo(jQuery("body")); if (settings.highlightrow) jQuery("tr:gt(" + (settings.headerrowsize - 1) + ")", jQuerytbl).hover(function() { jQuery(this).addClass(settings.highlightclass); }, function() { jQuery(this).removeClass(settings.highlightclass); }); jQuery(window).scroll(function() { if (jQuery.browser.msie && jQuery.browser.version == "6.0") jQueryclonedTable.css({ "filter": "alpha(opacity=85)", "position": "absolute", "top": jQuery(window).scrollTop(), "left": jQuerytbl.offset().left }); else jQueryclonedTable.css({ "position": "fixed", "top": "0", "left": jQuerytbl.offset().left - jQuery(window).scrollLeft() }); var sctop = jQuery(window).scrollTop(); var elmtop = jQuerytblhfixed.offset().top; if (sctop > elmtop && sctop <= (elmtop + jQuerytbl.height() - jQuerytblhfixed.height())) jQueryclonedTable.show(); else jQueryclonedTable.hide(); }); jQuery(window).resize(function() { if (jQueryclonedTable.outerWidth() != jQuerytbl.outerWidth()) { jQuerytblhfixed.find(headerelement).each(function(index) { var w = jQuery(this).width(); jQuery(this).css("width", w); jQueryclonedTable.find(headerelement).eq(index).css("width", w); }); jQueryclonedTable.width(jQuerytbl.outerWidth()); } jQueryclonedTable.css("left", jQuerytbl.offset().left); }); } }); function GetTblWidth(jQuerytbl) { var tblwidth = jQuerytbl.outerWidth(); return tblwidth; } };
