/*necessary for scroll*/
div.body_scroll {
height: 120px;
overflow-y: auto;
}
/*everything that follows is just to help the eye identify the table elements*/
table caption {
color:blue;
font-weight:bold;
text-align:center;
}
table tfoot, .table_footer {
color:orange;
}
table thead th {
font-weight:bold;
}
th, td {
padding:2px;
}
JavaScript
window.setTimeout(function(){
confirm('check out how our page looks before we inject the scrolling feature')
if ( $('table.body_scroll').length ) {
var $table_body_scroll=$('table.body_scroll'),
header_table=$( '<table aria-hidden="true" class="header_table"><thead><tr><td></td></tr></thead></table>' ),
scroll_div='<div class="body_scroll"></div>';
//inject table that will hold stationary row header; inject the div that will get scrolled
$table_body_scroll.before( header_table ).before( scroll_div );
$table_body_scroll.each(function (index) {
//to minimize FUOC, I like to set the relevant variables before manipulating the DOM
var columnWidths = [];
var $targetDataTable=$(this);
var $targetHeaderTable=$("table.header_table").eq(index);
var $targetDataTableFooter=$targetDataTable.find('tfoot');
// Get column widths
$($targetDataTable).find('thead tr th').each(function (index) {
columnWidths[index] = $(this).width();
});
//place target table inside of relevant scrollable div (using jQuery eq() and index)
$('div.body_scroll').eq(index).prepend( $targetDataTable ).width( $($targetDataTable).width()+25 );
// hide original caption, header, and footer from sighted users
$($targetDataTable).children('caption, thead, tfoot').hide();
// insert header data into static table
$($targetHeaderTable).find('thead').replaceWith( $( $targetDataTable ).children('caption, thead').clone().show() );
// modify column width for header
$($targetHeaderTable).find('thead tr th').each(function (index) {
$(this).css('width', columnWidths[index]);
});
// make sure table data still lines up correctly
$($targetDataTable).find('tbody tr:first td').each(function (index) {
$(this).css('width',...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.