0
Currently I have a table
fixing the thead
th
at the top of div
, but when scrolling (scrolling the page) add the edges of the th
. I need that when giving scroll continue with the edges.
$("#tabela").on("scroll",function(){
var tabela_divtop = $("table").closest($(this)).offset().top;
var tabela_top = $("table").offset().top;
var div_top = $(this).scrollTop();
var dist = tabela_top-tabela_divtop-div_top;
$("table thead th").css({
'top': dist <= 0 ? -dist-div_top+'px' : '0',
'z-index':'9',
'cursor':'pointer',
'border':'1px solid'
});
});
table{
font-size:12px!important;
border-width: 1px;
border-color: #A4A4A4;
border-collapse: collapse;
white-space: nowrap!important;
width: 100%;
padding: 0px!important;
text-align: center!important;
}
th{
white-space: nowrap!important;
font-size:12px!important;
background-color:#FFFFFF;
border-width: 1px;
padding: 1px!important;
border-style: solid;
border-color: #A4A4A4;
text-align:center!important;
}
tr{
background-color:#ffffff;
padding: 1px!important;
text-align: center!important;
}
td{
width:50px;
border-style: solid!important;
border-color: #A4A4A4!important;
font-size:12px!important;
border-width: 1px;
padding: 1px!important;
text-align: center!important;
}
*{
position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="large-12 medium-12 small-12 columns" id="tabela" style="overflow-y: scroll; height: 10%; border: 0px solid;">
<table>
<thead>
<tr>
<th>Titulo 1</th>
<th>Titulo 2</th>
<th>Titulo 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
<tr>
<td>teste 1</td>
<td>teste 2</td>
<td>teste 3</td>
</tr>
</tbody>
</table>
</div>
Without giving the scroll:
With scroll on page:
You can use the same css to place a fixed footer as well ?
– KevinF
Yes, I edited the answer for you to see, I left it with
position: sticky
also, but with the propertybottom: 0
that leaves it below the screen.– OtavioCapel