js固定表头

js固定表头


由于table的表头是js根据后台的数据直接动态拼接的html标签,所以Bootstrap的冻结表头用不了。

css代码片.

        #fixed_table #fix_tr1{
            background: #FFFFFF;
        }
        #fixed_table #fix_tr2{
            background: #FFFFFF;
        }
        #fixed_table{
            position: absolute;
            top: 0px;
            left: 0px;
            /*table-layout:fixed;*/
        }
        .scroll_table_content{
            width:100%;
            height:600px;
            overflow:auto;
        }
        #shelter{
            background-color:White
        }
        td{
            border:1px solid red;
        }
        #areas{
            padding: 0 10px;
            float: left;
            position: absolute;
            z-index: 999;
            background: #e5f1ff;
            border-right: 1px solid #ddd;
        }

html代码片.

<div class="scroll_table_content" id="scroll_wrap">
        <table id="top_fix_table" border="0" cellpadding="4" cellspacing="0" class="table table-hover" style="white-space: nowrap;">
            <thead id="theadid">
            <tr id="table_tr1">
            </tr>
            <tr id="table_tr2">
            </tr>
            </thead>
            <tbody id="tbody">
            </tbody>
        </table>
        <div id="test_width"></div>
    </div>

js代码片.

function frozenHeader() {
        $("#fix_theadid").html("");
        var sourceTable = $("#top_fix_table");
        var sourceTableHead = $("#theadid");
        var headHeight = sourceTableHead.height();
        var sourceTableWidth = sourceTable.outerWidth();
        var testDiv = $("#test_width"); 
        var wrapDiv = $("#scroll_wrap"); 
        var tempTop = wrapDiv.offset().top - $(window).scrollTop(); 
        $('body').append('<div id="shelter"><div id="fixed_table_wrap"><table id="fixed_table"  border="0" cellpadding="4" cellspacing="0" class="table table-hover" ><thead id="fix_theadid"></thead></table></div></div>');
        var fixTopDiv = $("#shelter");
        fixTopDiv.hide();
        fixTopDiv.css({
            'height': headHeight,
            'position': 'fixed',
            'top': tempTop + "px",
            'margin-left': 108+"px",
            'width': testDiv.width() + "px",
            'overflow': 'hidden'
        });
        $("#fixed_table_wrap,#fixed_table").css({'width': sourceTableWidth + "px"});
        $("#fixed_table_wrap,#fixed_table,#fix_theadid").css({'height': headHeight + "px"});
        Head = $("#table_tr1").clone().attr('id', 'fix_tr1');
        targetHead.appendTo('#fix_theadid');
        var targetHead2 = $("#table_tr2").clone().attr('id', 'fix_tr2');
        targetHead2.appendTo('#fix_theadid');
        $("#table_tr1 th").each(function(index,value){
            var tempWidth = $(value).outerWidth();
            var tempHeight = $(value).outerHeight();
            $("#fix_tr1 th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
        });
        $("#table_tr2 th").each(function(index,value){
            var tempWidth = $(value).outerWidth();
            var tempHeight = $(value).outerHeight();
            $("#fix_tr2 th").eq(index).css({'width':tempWidth+"px",'height':tempHeight+"px"});
        });
            wrapDiv.scroll(function () {
            var sl = -Math.max(wrapDiv.scrollLeft(), 
            $('document').scrollLeft());
            if (isNaN(sl)) {
                sl = -wrapDiv.scrollLeft();
            }
            fixTopDiv.css("left", sl + 'px');
            var scroll_top = wrapDiv.scrollTop() - headHeight + 
            $(window).scrollTop();
            tempTop = wrapDiv.offset().top - $(window).scrollTop();
            if (tempTop <= 0) {
                tempTop = 0;
            }
            var baseWidth = testDiv.width() + wrapDiv.scrollLeft();
            if (scroll_top >= 0) {
                fixTopDiv.css({'top': tempTop + "px", 'width': baseWidth + "px"});
                if (!fixTopDiv.is(':visible')) {
                    fixTopDiv.show();
                }
            } else {
                if (fixTopDiv.is(':visible')) {
                    fixTopDiv.hide();
                }

            }
        });
        
        $(window).on('scroll', function () {
            tempTop = wrapDiv.offset().top - $(window).scrollTop();
            var scroll_top = wrapDiv.scrollTop() - headHeight + 	          
            $(window).scrollTop();
            var baseWidth = testDiv.width() + wrapDiv.scrollLeft();
            if (tempTop <= 0) {
                tempTop = 0;
            }
            console.log(scroll_top);
            fixTopDiv.css({'top': tempTop + "px", 'width': baseWidth + "px"});
         
            if (scroll_top >= 0) {
                if (!fixTopDiv.is(':visible')) {
                    fixTopDiv.show();
                }
            } else {
                if (fixTopDiv.is(':visible')) {
                    fixTopDiv.hide();
                }
            }
        });
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值