JSFiddle - React, Tailwind, and code Playground
HTML
<table border="1" id='my_table'>
<tr>
<th>Column One Header</th>
<th>Column Two Header</th>
<th>Column Three Header</th>
<th>Column Four Header</th>
<th>Column Five Header</th>
<th>Column Six Header</th>
<th>Column Seven Header</th>
<th>Column Eight Header</th>
<th>Column Nine Header</th>
<th>Column Ten Header</th>
<th>Column One Header</th>
<th>Column Two Header</th>
<th>Column Three Header</th>
<th>Column Four Header</th>
<th>Column Five Header</th>
<th>Column Six Header</th>
<th>Column Seven Header</th>
<th>Column Eight Header</th>
<th>Column Nine Header</th>
<th>Column Ten Header</th>
<th>Column One Header</th>
<th>Column Two Header</th>
<th>Column Three Header</th>
<th>Column Four Header</th>
<th>Column Five Header</th>
<th>Column Six Header</th>
<th>Column Seven Header</th>
<th>Column Eight Header</th>
<th>Column Nine Header</th>
<th>Column Ten Header</th>
<th>Column One Header</th>
<th>Column Two Header</th>
<th>Column Three Header</th>
<th>Column Four Header</th>
<th>Column Five Header</th>
<th>Column Six Header</th>
<th>Column Seven Header</th>
<th>Column Eight Header</th>
<th>Column Nine Header</th>
<th>Column Ten Header</th>
<th>Column One Header</th>
<th>Column Two Header</th>
<th>Column Three Header</th>
<th>Column Four Header</th>
<th>Column Five Header</th>
<th>Column Six Header</th>
<th>Column Seven Header</th>
<th>Column Eight Header</th>
...
CSS
th {background-color:#00F;}
JavaScript
$(document).ready(function(){
$("#my_table").sticky();
});
(function($)
{
$.fn.sticky = function()
{
var tableId = this;
//set border collapse
//$(tableId).css("border-collapse", "collapse");
//collect widths of all the <th> elements
var tableWidth = $(tableId).width();
//get the table height to stop sticky after table has been scrolled
var tableHeight = $(tableId).height();
//get the height of the table header row to offest it from the stop-sticky-after-table-scroll feature
var thHeight = $(tableId).find("th:first").parent().height();
var thWidthsArr = [];
$(tableId).find("th").each(function(){
thWidthsArr.push($(this).css("width"));
});
var pos = $(tableId).offset();
//set the distance of the table from the top,
//we ll need to make the headers sticky when this distance is 0
var thTop = pos.top + "px";
var thLeft = pos.left;
//identify first table row
var firstRow = "tr:first-child";
//change firstRow in case there is a thead tag used
var hasTHead = $(tableId).find("thead").length;
if(hasTHead) firstRow = "thead tr:first-child";
//set the widths of the first and last tr's ths/tds...
//this is done coz in some cases,
//the widths will get messed up if the was generated dynamically
var count = 0;
$(tableId).find(firstRow + ">th").each(function(){
$(this).css("width", thWidthsArr[count]);
count++;
});
count = 0;
//assign the widths to td elements of a tr row
//choose the last tr coz the first tr has th elements
var lastRow = "tr:last-child";
$(tableId).find(lastRow + ">td").each(function(){
$(this).css("width", thWidthsArr[count]);
count++;
});
$(window).scroll(function(){
if($(window).scrollTop()...