scroll problem with fixed header
by uzbeki
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="index.js" type='module'></script>
</head>
<body>
<div id="root">
<main>
<section id="result">
<div class="table">
<table role="table">
<thead>
<tr role="row">
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
ステータス<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
種類<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
事業所CD<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
発注番号<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
発注日<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
加工部品<span></span></th>
<th colspan="1" role="columnheader" title="Toggle SortBy" style="cursor: pointer;">
加工部品名<span></span></th>
<th colspan="1" role="columnheader" title="Toggle...
CSS
#result {
--body-bg: #ffffff;
--header-bg: #e2efda;
--title-color: #0070c0;
--table-head-bg: #d9e1f2;
--button-bg: #bfbfbf;
--content-margin: 5vw;
--ugly-yellow: #fff2cc;
font-size: large;
cursor: auto;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans",
"Droid Sans", "Helvetica Neue", sans-serif;
-webkit-font-smoothing: antialiased;
padding: 0;
box-sizing: border-box;
margin: 1rem 0;
position: relative;
}
.table {
font-size: large;
cursor: auto;
margin: 0;
padding: 0;
box-sizing: border-box;
max-height: 520px !important;
overflow: auto;
max-width: 100%;
}
table {
cursor: auto;
margin: 0;
padding: 0;
box-sizing: border-box;
min-width: 100%;
border-collapse: collapse;
text-align: center;
font-size: small;
}
th {
border-collapse: collapse;
font-size: small;
margin: 0;
box-sizing: border-box;
border: 1px solid #ccc;
padding: 0.5rem;
user-select: none;
position: sticky;
top: 0;
border-top: 0 solid black;
z-index: 1;
background-color: var(--table-head-bg);
word-break: keep-all;
white-space: nowrap;
cursor: pointer;
}
table tbody tr:focus-within,
:focus-visible,
:focus {
outline: 1px solid red;
}
table tbody tr.text-blue td:first-child {
color: blue;
}
td {
border-collapse: collapse;
text-align: center;
font-size: small;
cursor: pointer;
margin: 0;
box-sizing: border-box;
border: 1px solid #ccc;
padding: 0.5rem;
user-select: none;
}
JavaScript
const handleKeyDown = e => {
// when shift or tab is pressed, do not prevent default behavior
if (![16, 9].includes(e.keyCode)) {
e.stopPropagation();
e.preventDefault();
}
const currentRow = e.target.closest("tr");
switch (e.keyCode) {
case 40: // down
currentRow?.nextElementSibling?.focus();
break;
case 38: // up
currentRow?.previousElementSibling?.focus();
break;
default:
break;
}
};
document.querySelectorAll("tbody tr").forEach(row => (row.onkeydown = handleKeyDown));