0

У меня есть таблица с фиксированным заголовком,которая находится в блоке.При скорлле эта шапка выходит заграницы блока. У меня нет идей как этого избежать. Буду рад любым советам

Вот мой html

        <table class=" fixtable table-body">
            <thead class="thead">
                <tr class="title-row">
                    <th class="title-th">Артикул</th>
                    <th class="title-th">Текущая МРЦ</th>
                    <th class="title-th">Новая РРЦ</th>
                </tr>
            </thead>
            <tbody class="tbody">
                <tr class="table-row">
                    <td class="td-body">2934/8C</td>
                    <td class="td-body">20 369,00</td>
                    <td class="td-body">14 258,30</td>
                </tr>
                <tr class="table-row">
                    <td class="td-body">2756/6C</td>
                    <td class="td-body">16 276,00</td>
                    <td class="td-body">11 393,20</td>
                </tr>     
          
                <tr class="table-row">
                    <td class="td-body">2934/8C</td>
                    <td class="td-body">20 369,00</td>
                    <td class="td-body">14 258,30</td>
                </tr>
        </table> 
       </div> 
    </div> 
</div>

мои стили

.fixtable-fixed {
    position: fixed;
    top: 0;
    z-index: 101;
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 200px;
  overflow: hidden;
  
}

.layer {
    overflow: scroll; /* Добавляем полосы прокрутки */
    width: 600px; /* Ширина блока */
    height: 300px; /* Высота блока */
    padding: 5px; /* Поля вокруг текста */
    border: solid 1px black; /* Параметры рамки */
   } 

.scroll-table {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  width: 100%;
  font-size: 2.5rem;
  margin-top: 10px;
  margin-bottom: 15px;
  position: relative;
  box-sizing: border-box;
}

.thead
{
  width: 100%;
  max-width: inherit;
}

.thead-fixed
{
  /* position: fixed;
  width: 100%;
  max-width: inherit; 
  top:0px; */
 

  
}

.preloader_table
{
  position: absolute;
  background-color: brown;
  width: 100%;
  height: 100%;
  z-index:1000;
}

.scroll-table-body {
  /* width:80%; */
  color: black;
  overflow: auto;
  height: 300px; 
  display: flex;
  justify-content: flex-end;
  align-items: end;
}

.title-row
{
  display: flex;
}

.title-th {
  display: flex;
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 200px;

  top: 0;
  font-family: 'Roboto-Condensed';
  justify-content: center;
  align-items: center;

  
}

.title-th_big {
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 6.6666%;
  
  
}

.title-table {
  background: #969595;
  color: black;
  font-size: 2rem;
}



.table-row > td {
  border-right: 1px solid #5a5959;
  padding: 5px 10px;
  border-bottom: 1px solid #5a5959;
  font-size: 1.5rem;
  font-family: 'Roboto-Condensed';
  
}

.table-row {
  display: flex;
  background: white;
}

.td-body
{
  width: 200px;
  border: 1px solid #5a5959;
  text-align: center;
  font-size: 2.3rem;
}

.table-row>.td-body
{
  text-align: left;
}

.table-row:nth-child(2n) {
  background: #d3d6d5;
}

.table-row:hover {
  background-color: #f3cf85;
}

вот мой jquery

function FixTable(table) {
    var inst = this;
    this.table  = table;
 
    $('tr > th',$(this.table)).each(function(index) {
        var div_fixed = $('<div/>').addClass('fixtable-fixed');
        var div_relat = $('<div/>').addClass('fixtable-relative');
        div_fixed.html($(this).html());
        div_relat.html($(this).html());
        $(this).html('').append(div_fixed).append(div_relat);
        $(div_fixed).hide();
    });
 
    this.StyleColumns();
    this.FixColumns();
 
    $('.layer').scroll(function(){
        inst.FixColumns()
    }).resize(function(){
        inst.StyleColumns()
    });
}
 
FixTable.prototype.StyleColumns = function() {
    var inst = this;
    $('tr > th', $(this.table)).each(function(){
        var div_relat = $('div.fixtable-relative', $(this));
        var th = $(div_relat).parent('th');
        $('div.fixtable-fixed', $(this)).css({
        
        });
    });
}
 
FixTable.prototype.FixColumns = function() {
    var inst = this;
    var show = false;
    var s_top = $(window).scrollTop();
    var h_top = $(inst.table).offset().top;
    console.log('s_top:',s_top);
    console.log('результат вычисления');
   
    if (s_top < (h_top + $(inst.table).height() - $(inst.table).find('.fixtable-fixed').outerHeight()) && s_top > h_top) {

       
        show = true;
    }
 
    $('tr > th > div.fixtable-fixed', $(this.table)).each(function(){
        show ? $(this).show() : $(this).hide();
        console.log('скрыто')
    });
}
 
$(document).ready(function(){
    $('.fixtable').each(function() {
        new FixTable(this);
    });
});

https://codepen.io/sergejskolodkin/pen/MWEYmRZ

2 ответа 2

1

Воспользоваться position: sticky; top: 0; Если поддержка только новых браузеров. Вот пример

1

Вот решение с position sticky

.fixtable-fixed {
    position: sticky;
    top: 0;
    z-index: 101;
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 200px;
  overflow: hidden;
  
}

.layer {
    overflow: scroll; /* Добавляем полосы прокрутки */
    width: 600px; /* Ширина блока */
    height: 300px; /* Высота блока */
    padding: 5px; /* Поля вокруг текста */
    border: solid 1px black; /* Параметры рамки */
   } 

.scroll-table {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  width: 100%;
  font-size: 2.5rem;
  margin-top: 10px;
  margin-bottom: 15px;
  position: relative;
  box-sizing: border-box;
}

.thead
{
  width: 100%;
  max-width: inherit;
  position: sticky;
    top: 0;
}

.thead-fixed
{
  /* position: fixed;
  width: 100%;
  max-width: inherit; 
  top:0px; */
 

  
}

.preloader_table
{
  position: absolute;
  background-color: brown;
  width: 100%;
  height: 100%;
  z-index:1000;
}

.scroll-table-body {
  /* width:80%; */
  color: black;
  overflow: auto;
  height: 300px; 
  display: flex;
  justify-content: flex-end;
  align-items: end;
}

.title-row
{
  display: flex;
}

.title-th {
  display: flex;
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 200px;

  top: 0;
  font-family: 'Roboto-Condensed';
  justify-content: center;
  align-items: center;

  
}

.title-th_big {
  border: 1px solid #5a5959;
  background: #d3d6d5;
  padding: 5px 10px;
  width: 6.6666%;
  
  
}

.title-table {
  background: #969595;
  color: black;
  font-size: 2rem;
}



.table-row > td {
  border-right: 1px solid #5a5959;
  padding: 5px 10px;
  border-bottom: 1px solid #5a5959;
  font-size: 1.5rem;
  font-family: 'Roboto-Condensed';
  
}

.table-row {
  display: flex;
  background: white;
}

.td-body
{
  width: 200px;
  border: 1px solid #5a5959;
  text-align: center;
  font-size: 2.3rem;
}

.table-row>.td-body
{
  text-align: left;
}

.table-row:nth-child(2n) {
  background: #d3d6d5;
}

.table-row:hover {
  background-color: #f3cf85;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.