Table fixed header and scrollable body

前端 未结 29 1869
粉色の甜心
粉色の甜心 2020-11-22 05:33

I am trying to make a table with fixed header and a scrollable content using the bootstrap 3 table. Unfortunately the solutions I have found does not work with bootstrap or

29条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 05:51

    For tables that are full height (the page scrolls, not the table)

    Note: I move the whole ... beause In my case I had two rows (Title and filters)

    With JS (jQuery)

    $( function() {
    
                let marginTop = 0; // Add margin if the page has a top nav-bar
                let $thead = $('.table-fixed-head').find('thead');
                let offset = $thead.first().offset().top - marginTop;
                let lastPos = 0;
    
                $(window).on('scroll', function () {
    
                    if ( window.scrollY > offset )
                    {
                        if ( lastPos === 0 )
                        {
                            // Add a class for styling
                            $thead.addClass('floating-header');
                        }
    
                        lastPos = window.scrollY - offset;
                        $thead.css('transform', 'translateY(' + ( lastPos ) + 'px)');
                    }
                    else if ( lastPos !== 0 )
                    {
                        lastPos = 0;
                        $thead.removeClass('floating-header');
                        $thead.css('transform', 'translateY(' + 0 + 'px)');
                    }
                });
    });
    

    CSS (Just for styling)

     thead.floating-header>tr>th {
           background-color: #efefef;
     }
    
    thead.floating-header>tr:last-child>th {
           border-bottom: 1px solid #aaa;
    }
    

提交回复
热议问题