Fix thead on page scroll

前端 未结 4 1211
太阳男子
太阳男子 2020-12-14 04:22

Situation: A page with a table with several rows. I want to fix thead when thead reach the top of the page when scrolling, using jquery or any given scripting. I don\'t wan

4条回答
  •  心在旅途
    2020-12-14 05:03

    (function ($) {
        $.fn.fixedHeader = function () {
            return this.filter('table').each(function () {
                var that = this;
                var $head = $('
    ').addClass('head'); $(this).before($head); $('th', this).each(function () { var $el = $(this); var $div = $('
    ').width($el.outerWidth()).text( $el.text()); $head.append($div); }); $(window).on('scroll', function () { var $that = $(that); var w = $(window).scrollTop(); var o = $('th', that).first().offset().top; var tableO = $that.offset().top + $that.height(); if (o < w && tableO > w) { $head.show(); } else { $head.hide(); } }); }); }; })(jQuery);

    You can use this jquery plugin (you need to adapt .head style to your thead style). Working example here: http://jsfiddle.net/lukasi/7K52p/1/

提交回复
热议问题