I have a table in which I need to put a fixed header when it moves down, because there is so much data to show. The problem is that I have many columns to display, and these columns span more than the width of the screen.
I have already searched for many ways to do what I'm looking for, I try to do it with Jquery
, but if you lower it a little, the header goes crazy, and when it goes down the header is kept, but the columns of the header no longer match with the content columns, and I did not manage to make the header move according to the horizontal scroll.
In this link I have an example of what I "managed" to do for now:
var stickyOffset = $('.sticky').offset().top;
$(window).scroll(function() {
var sticky = $('.sticky'),
scroll = $(window).scrollTop();
if (scroll >= stickyOffset) sticky.addClass('fixed');
else sticky.removeClass('fixed');
});
.fixed {
position: fixed;
top: 0;
left: 40;
}
body {
padding: 0 20px;
font-family: Arial, Helvetica, sans-serif;
font-size: 11px;
}
table {
border-width: 1;
border-style: solid;
padding: 0;
margin: 0 0 200px 0;
border-collapse: collapse;
}
th {
padding: 5px;
/*NOTE: th padding must be set explicitly in order to support IE*/
text-align: right;
font-weight: bold;
line-height: 2em;
color: #FFF;
background-color: #b1b1b1;
}
tbody td {
padding: 10px;
line-height: 18px;
border-top: 1px solid #E0E0E0;
}
tbody tr:nth-child(2n) {
background-color: #F7F7F7;
}
tbody tr:hover {
background-color: #EEEEEE;
}
td {
text-align: right;
}
td:first-child,
th:first-child {
text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<p>Contenido</p>
<p>Contenido</p>
<p>Contenido</p>
<p>Contenido</p>
<p>Contenido</p>
<p>Contenido</p>
</body>
<table border="1">
<tr class="sticky">
<th>Id</th>
<th>Cliente</th>
<th>Descripcion</th>
<th>Notas</th>
<th>Fecha de llegada</th>
<th>Fecha ejecucion</th>
<th>Usuario</th>
<th>Informacion</th>
<th>Informacion</th>
<th>Informacion</th>
<th>Informacion</th>
<th>Informacion</th>
<th>Informacion</th>
<th>Informacion</th>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
<tr>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
<td>Datos</td>
</tr>
</table>
</html>