thead位置绝对不能在IE7中工作

时间:2012-05-27 23:20:26

标签: jquery html-table internet-explorer-7 fixed-header-tables

我尝试在页面上缩小时修复表头。问题出在IE7中, thead 不是绝对的,而是显示在“正确”的位置。它需要飞过桌子。它在FireFox 11中非常完美

我有一个表结构如下:

<span id="MyTable">
    <table border="0" cellpadding="2" cellspacing="2" width="100%">
    <thead class="tableFloatingHeader">
    <tr>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
    </tr>
    <tr>
    <td><b>Total</b></td>
    <td></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    </tr>
    </thead>
<thead class="tableFloatingHeaderOriginal">
    <tr>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
        <th>t</th>
    </tr>
    <tr>
    <td><b>Total</b></td>
    <td></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    <td align="right"><b>2</b></td>
    </tr>
    </thead>

CSS

.tableFloatingHeader {
    overflow: hidden;
    position: absolute;
    top: 0;
    display: none;
    left: auto;
    background-color: red;
}

JQuery的

<script type="text/javascript">
        function UpdateTableHeaders() {
            $("div.divTableWithFloatingHeader").each(function() {
                var originalHeaderRow = $(".tableFloatingHeaderOriginal");
                var floatingHeaderRow = $(".tableFloatingHeader");
                var offset = $(this).offset();
                var scrollTop = $(window).scrollTop();
                if ((scrollTop > offset.top) && (scrollTop < offset.top + $(this).height())) {
                    floatingHeaderRow.css("display", "block");
                    floatingHeaderRow.css("top", Math.min(scrollTop - offset.top, $(this).height() - floatingHeaderRow.height()) + "px");

                    // Copy cell widths from original header
                    $("th", floatingHeaderRow).each(function(index) {
                        var cellWidth = $("th", originalHeaderRow).eq(index).css('width');
                        $(this).css('width', cellWidth);
                    });

                    // Copy row width from whole table
                    floatingHeaderRow.css("width", $(this).css("width"));
                }
                else {
                    floatingHeaderRow.css("display", "none");
                    floatingHeaderRow.css("top", "0px");
                }
            });
        }
        UpdateTableHeaders();
        $(window).scroll(UpdateTableHeaders);
        $(window).resize(UpdateTableHeaders);
    </script>

1 个答案:

答案 0 :(得分:1)

这可能对你有所帮助,基本上为IE7添加一个条件CSS表达式样式。我不喜欢使用表达式,但IE7是另一种动物。

thead tr {
     position: relative;
     top: expression(this.offsetParent.scrollTop);
    }

http://geekswithblogs.net/LSheu/archive/2009/01/30/css-table-scrolling-with-fixed-header-for-ie-7.aspx

修改*错误的网址