0

I am trying to fix table heading on top during Scroll. It is a Bootstrap theme and i don't want to use any plugin. I have tried with the below code. table heading fix is working with Scroll.

But it is leaving CSS when scrolling.

enter image description here

No custom CSS used from my side. only Bootstrap Stuff used.

HTML

<div class="table-responsive">
    <table class="table table-striped persist-area">
        <thead>
            <tr class="persist-header">
                <th>........
                </th>
                <th>........
                </th>
                <th>........
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>.....
            </tr>
            <tr>....
            </tr>
            <tr>....
            </tr>
        <tbody>
    </table>
</div>

JavaScript

 function UpdateTableHeaders() {
                var el             = $('.persist-area');
                   offset         = el.offset();
                   scrollTop      = $(window).scrollTop();
                   floatingHeader = $(".floatingHeader");

               if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
                   floatingHeader.css({
                    "visibility": "visible"
                   });

               } else {
                   floatingHeader.css({
                    "visibility": "hidden"
                   });      
               };

        }


        $(function() {
            var clonedHeaderRow;
            clonedHeaderRow = $(".persist-header");
            clonedHeaderRow
            .before(clonedHeaderRow.clone())
            .css({"width" : clonedHeaderRow.width()})
                .addClass("floatingHeader");

            if($(".persist-header").hasClass('floatingHeader')){
                $(".persist-header th").each(function() {
                    clonedHeaderRowTh = $(this);
                });
            }

            $(window).scroll(UpdateTableHeaders).trigger("scroll");

        });
juggler
  • 60
  • 1
  • 7
  • No custom CSS used from my side. only Bootstrap Stuff used. – juggler Jul 28 '14 at 18:46
  • 1
    Did you try that? http://getbootstrap.com/components/#navbar-fixed-top – Ramy Nasr Jul 28 '14 at 18:46
  • sorry its my mistake.. it is a table heading not a Navigation .. question is updated. – juggler Jul 28 '14 at 18:51
  • [Look at this question](http://stackoverflow.com/questions/4709390/table-header-to-stay-fixed-at-the-top-when-user-scrolls-it-out-of-view-with-jque) It looks like exactly the same of what you are asking. – Rick Bronger May 18 '16 at 06:42

0 Answers0