[javascript] How can I lock the first row and first column of a table when scrolling, possibly using JavaScript and CSS?

I did this with a combination of:

  • Using multiple tables
  • Fixed-size cells
  • jQuery's scrollTop and scrollLeft functions

Here's a jsfiddle example to demonstrate.

Haven't tested on all browsers but I imagine it's not great on older IE versions.

_x000D_
_x000D_
$("#clscroll-content").scroll(function() {_x000D_
    $("#clscroll-row-headers").scrollTop($("#clscroll-content").scrollTop());_x000D_
    $("#clscroll-column-headers").scrollLeft($("#clscroll-content").scrollLeft());_x000D_
});_x000D_
_x000D_
$("#clscroll-column-headers").scroll(function() {_x000D_
    $("#clscroll-content").scrollLeft($("#clscroll-column-headers").scrollLeft());_x000D_
});_x000D_
_x000D_
$("#clscroll-row-headers").scroll(function() {_x000D_
    $("#clscroll-content").scrollTop($("#clscroll-row-headers").scrollTop());_x000D_
});
_x000D_
.clscroll table {_x000D_
    table-layout: fixed;_x000D_
}_x000D_
_x000D_
.clscroll td, .clscroll th { _x000D_
    overflow: hidden;_x000D_
}_x000D_
_x000D_
.corner-header {_x000D_
    float: left;_x000D_
}_x000D_
_x000D_
.column-headers {_x000D_
    float: left;_x000D_
    overflow: scroll;_x000D_
}_x000D_
_x000D_
.row-headers {_x000D_
    clear: both;_x000D_
    float: left;    _x000D_
    overflow: scroll;_x000D_
}_x000D_
_x000D_
.table-content {_x000D_
    table-layout: fixed;_x000D_
    float: left;_x000D_
    overflow: scroll;_x000D_
}_x000D_
_x000D_
.clscroll td, .clscroll th { _x000D_
    width: 200px;_x000D_
    border: 1px solid black;_x000D_
}_x000D_
_x000D_
.row-headers, .table-content {_x000D_
    height: 100px;_x000D_
}_x000D_
_x000D_
.column-headers, .table-content, .table-content table, .column-headers table {_x000D_
    width: 400px;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div class="clscroll corner-header">_x000D_
  <table>_x000D_
      <tr>_x000D_
          <th>&nbsp;</th>_x000D_
      </tr>_x000D_
  </table>_x000D_
</div>_x000D_
<div class="clscroll column-headers" id="clscroll-column-headers">_x000D_
  <table>_x000D_
      <tr>_x000D_
          <th>Bus</th>_x000D_
          <th>Plane</th>_x000D_
          <th>Boat</th>_x000D_
          <th>Bicycle</th>_x000D_
      </tr>_x000D_
  </table>_x000D_
</div>_x000D_
<div class="clscroll row-headers" id="clscroll-row-headers">_x000D_
  <table>_x000D_
      <tr>_x000D_
          <th>Red</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Green</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Blue</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Orange</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Purple</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Yellow</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Pink</th>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <th>Brown</th>_x000D_
      </tr>_x000D_
  </table>_x000D_
</div>_x000D_
<div class="clscroll table-content" id="clscroll-content">_x000D_
  <table>_x000D_
      <tr>_x000D_
          <td>Red Bus</td>_x000D_
          <td>Red Plane</td>_x000D_
          <td>Red Boat</td>_x000D_
          <td>Red Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Green Bus</td>_x000D_
          <td>Green Plane</td>_x000D_
          <td>Green Boat</td>_x000D_
          <td>Green Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Blue Bus</td>_x000D_
          <td>Blue Plane</td>_x000D_
          <td>Blue Boat</td>_x000D_
          <td>Blue Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Orange Bus</td>_x000D_
          <td>Orange Plane</td>_x000D_
          <td>Orange Boat</td>_x000D_
          <td>Orange Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Purple Bus</td>_x000D_
          <td>Purple Plane</td>_x000D_
          <td>Purple Boat</td>_x000D_
          <td>Purple Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Yellow Bus</td>_x000D_
          <td>Yellow Plane</td>_x000D_
          <td>Yellow Boat</td>_x000D_
          <td>Yellow Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Pink Bus</td>_x000D_
          <td>Pink Plane</td>_x000D_
          <td>Pink Boat</td>_x000D_
          <td>Pink Bicycle</td>_x000D_
      </tr>_x000D_
      <tr>_x000D_
          <td>Brown Bus</td>_x000D_
          <td>Brown Plane</td>_x000D_
          <td>Brown Boat</td>_x000D_
          <td>Brown Bicycle</td>_x000D_
      </tr>_x000D_
  </table>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to excel

Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support Converting unix time into date-time via excel How to increment a letter N times per iteration and store in an array? 'Microsoft.ACE.OLEDB.16.0' provider is not registered on the local machine. (System.Data) How to import an Excel file into SQL Server? Copy filtered data to another sheet using VBA Better way to find last used row Could pandas use column as index? Check if a value is in an array or not with Excel VBA How to sort dates from Oldest to Newest in Excel?

Examples related to scroll

How to window.scrollTo() with a smooth effect Angular 2 Scroll to bottom (Chat style) Scroll to the top of the page after render in react.js Get div's offsetTop positions in React RecyclerView - How to smooth scroll to top of item on a certain position? Detecting scroll direction How to disable RecyclerView scrolling? How can I scroll a div to be visible in ReactJS? Make a nav bar stick Disable Scrolling on Body

Examples related to css-tables

How (and why) to use display: table-cell (CSS) height: 100% for <div> inside <div> with display: table-cell css display table cell requires percentage width space between divs - display table-cell Why is width: 100% not working on div {display: table-cell}? Controlling Spacing Between Table Cells Setting a max height on a table How to apply CSS page-break to print a table with lots of rows? How is a CSS "display: table-column" supposed to work? What replaces cellpadding, cellspacing, valign, and align in HTML5 tables?