UNPKG

jquery-table-fixed-header

Version:

A jQuery plugin to make table header fixed on the top when scrolling down.

2 lines (1 loc) 3.71 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("jquery")):"function"==typeof define&&define.amd?define(["jquery"],t):e["jquery-table-fixed-header"]=t(e.jQuery)}(this,function(w){"use strict";w=w&&w.hasOwnProperty("default")?w["default"]:w;var n=Boolean(window.ActiveXObject&&!window.XMLHttpRequest),e=Boolean(window.ActiveXObject&&window.XMLHttpRequest&&!document.documentMode),t=Boolean(window.ActiveXObject&&window.XMLHttpRequest&&document.documentMode&&!window.XMLSerializer),o={headerRows:1,fixedClass:"table-fixed-header regular-table-fixed-header",fixedTop:0};function r(e){"function"!=typeof e.fixedTop&&(e.fixedTop=parseInt(e.fixedTop))}var a=/\s+/g,d=" ",l=".";function f(e,t){var n=function i(e){return(d+e).replace(a,l)}(t);return e.filter("table:not("+n+")")}function x(e,t){return e.find("tr:lt("+t+")")}var b={margin:"0",padding:"0","table-layout":"fixed",visibility:"hidden",position:"fixed"};var C=window.getComputedStyle?function(e){var t=window.getComputedStyle(e[0]).width;return parseFloat(t)}:e?function(e){var t=parseInt(e.css("border-left-width"))||0,n=parseInt(e.css("border-right-width"))||0;return e.width()+(t+n)/2}:function(e){return e.width()},y=e||t?function(e,t){e.width(t.outerWidth())}:function(e,t){e.width(t.width())};var m=w([]);function s(e,t,n,a,i){var d=w(e),l={$table:d,options:t,$scrollContainer:m},o=t.headerRows,f=t.fixedTop,r=x(d,o);if(r.length){var s=r.parent(),c=l.$scrollContainer=n(l);if(c.length){var u=function v(e,t){var n=t.headerRows,i=t.fixedClass,o=e.clone(),r=x(o,n),a=r.parent();return o.find("tr").not(r).remove(),o.children().not(a).remove(),o.removeAttr("id").find("[id]").removeAttr("id"),o.addClass(i),o.css(b),o}(d,t);d.after(u);var h=!1,p=function(){if(!h&&!d.is(":hidden")){h=!0,function r(e,o){e.each(function(e,t){var n=w(t),i=o.eq(e);y(n.parent(),i.parent()),n.children().each(function(e,t){var n=w(t),o=i.children().eq(e);n.children().each(function(e,t){var n=w(t),i=o.children().eq(e);n.width(C(i))})})})}(u.children(),s);var e=function o(e){return"function"==typeof e?e():e}(f),t=c.scrollTop()+e,n=d[0].offsetTop;if(n<=t&&t+u.outerHeight()<=n+d.outerHeight()){var i=a(e,l);i.visibility="visible",u.css(i)}else u.css("visibility","hidden");h=!1}};i(p,l),p()}}}var c=w(window);var u={headerRows:1,fixedClass:"table-fixed-header container-table-fixed-header",fixedTop:0,scrollContainer:""},h=w(window);return w.fn.tableFixedHeader=function(e){return n?this:e&&e.scrollContainer?function t(e){var n=w.extend({},u,this.data(),e);return r(n),f(this,n.fixedClass).each(function(e,t){s(t,n,function(e){var t=e.$table,n=e.options,i=t.closest(n.scrollContainer).eq(0);if(i.length){var o=i.css("position");""!==o&&"static"!==o||i.css("position","relative")}return i},function(e,t){var n,i,o,r,a=t.$table,d=t.$scrollContainer,l=a.outerWidth(),f=d[0].clientWidth-a[0].offsetLeft+d.scrollLeft();i=f<l?(n=f+"px",l-f+"px"):(n="auto","0");var s=d.scrollLeft()-a[0].offsetLeft;return r=0<s?o=s+"px":(o="auto","0"),{top:Math.round(d.offset().top-h.scrollTop()+e)+"px",left:a.offset().left-h.scrollLeft()+"px",clip:"rect(auto "+n+" auto "+o+")","clip-path":"inset(0 "+i+" 0 "+r+")"}},function(e,t){var n=t.$scrollContainer;h.on("scroll",e),h.on("resize",e),n.on("scroll",e),n.on("resize",e)})}),this}.call(this,e):function i(e){var n=w.extend({},o,this.data(),e);return r(n),f(this,n.fixedClass).each(function(e,t){s(t,n,function(){return c},function(e,t){var n=t.$table,i=t.$scrollContainer;return{top:e+"px",left:n.offset().left-i.scrollLeft()+"px"}},function(e,t){var n=t.$scrollContainer;n.on("scroll",e),n.on("resize",e)})}),this}.call(this,e)},function i(){w("table.fixed-header").tableFixedHeader()}(),w});