单行固定表头

来源:互联网 发布:mac用手机app 编辑:程序博客网 时间:2024/06/07 05:57
JsUtil.fixed = {
    FixTableHeadRow: function(table) {
        var table = JsUtil.dom.getObj(table);
        if (table) {
            JsUtil.fixed._f("_" + table.id, table);
        } else {
            $(".common_table[display!='none'][name!='noFix'] ").each(function(index, obj) {
                JsUtil.fixed._f(index, obj);//不传table对象
            });
        }

    },

    _f: function(index, obj) {
        if ("_rightTable" == index) {
            return;
        }
        var parent = $(obj).parent();
        if (parent.is('DIV')) {
            $(parent.children()).each(function(){
                if (this.id && new RegExp('^fixTableHeader').test(this.id)) {
                    $(this).remove();
                }
            });
            parent.scrollLeft = 0 + "px";
            var isScroll = JsUtil.dom.isScroll(parent.get(0));
            var w = parent.css("width").replace("px", "");
            var fixTr = $(obj).find("tr:eq(0)");
            var obj1=obj;
            if (fixTr.length > 0) {
               var th_mTop = fixTr.position().top - parent.position().top - 2.5 + (parent.get(0).scrollTop - 1);
               var thDiv = document.getElementById("fixTableHeader" + index);
                if (thDiv == null) {
                    thDiv = document.createElement("div");
                    $(parent).prepend(thDiv);
                    $(parent).bind("scroll", {
                        "tIndex": index
                    },
                    JsUtil.fixed.fixTableOnScroll);
                }
                thDiv.id = "fixTableHeader" + index;
                thDiv.style.position = "fixed";
                thDiv.style.whiteSpace = "nowrap";
                thDiv.style.height = "29px";
                thDiv.style.lineHeight = "29px";
                thDiv.style.background = "#FFFFFF";
                thDiv.style.overflow = "hidden";
                /*thDiv.style.marginTop = th_mTop + "px";*/
                thDiv.style.marginLeft = "0px";
                thDiv.style.width = (isScroll ? (parseFloat(w) - 17) : w) + "px";
                thDiv.innerHTML = "";
                
                var thArray = new Array();
                $(fixTr).find("th").each(function(index, obj) {
                    if ($(obj).css("display") !== 'none') {
                        thArray.push(obj);
                    }
                });
                var size = thArray.length;
                $(thArray).each(function(index, obj) {
                var cW = $(obj).css("width").replace("px","");
    var wt2 = (cW == null || cW == undefined || cW == "auto") ? (obj.clientWidth - 4): cW;
                    var th = document.createElement("div");
                    th.style.background = "#e0f2fb";
                    th.style.display = "inline-block";
                    th.style.height = "100%";
                    th.style.border = "1px solid #ccc";
                    th.style.borderRight = "0px";
                    th.style.verticalAlign = "middle";
                    th.style.align = "center";
                    th.style.padding = "0 10px 0 10px";
                    if($(obj1).hasClass('common_table_nbfl')){
                    if(index == 0){
                    th.style.borderLeft = "0";
                    }
                    }else{
                    if (index == size - 1) {
                            th.style.borderRight = "1px solid #ccc";
                        }
                    }
                    th.style.width = wt2 + "px";
                    th.innerHTML = obj.innerHTML;
                    thDiv.appendChild(th);
                });
            }
        }
    },
    fixTableOnScroll: function(event) {
        document.getElementById("fixTableHeader" + event.data.tIndex).scrollLeft = this.scrollLeft;
    }

}


只能固定单行th可以

未完待续----------------------------------------------------------------------------------------------------------------

0 0