使表格标题滚动页面

问题描述 投票:1回答:2

我有一张桌子:

<table class="table">
    <div id="table-header">
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
    </div>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
</table>

我想让我的表格标题贴在页面顶部,当我有更多行时滚动。我从w3schools尝试了以下示例:

// When the user scrolls the page, execute myFunction 
window.onscroll = function() {myFunction()};

// Get the header
var header = document.getElementById("table-header");

// Get the offset position of the navbar
var sticky = header.offsetTop;

// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
    if (window.pageYOffset >= sticky) {
        header.classList.add("sticky");
    } else {
        header.classList.remove("sticky");
    }
}

这是CSS:

.sticky {
    position: fixed;
    top: 0;
    width: 100%
}

我觉得我有逻辑,但我不确定为什么这不起作用。我猜它与CSS有关?

html css
2个回答
1
投票

scroll上使用window事件处理程序,并使用另一个具有固定位置的table来显示页面顶部的标题。

试试这个 :-

HTML

<table id="fixed-header"></table>

CSS

#fixed-header {
    position: fixed;
    top: 0px; display:none;
    background-color:white;
}

JAVASCRIPT

var tableOffset = $("#table-1").offset().top;
var $header = $("#table-1 > thead").clone();
var $fixedHeader = $("#fixed-header").append($header);

$(window).bind("scroll", function() {
    var offset = $(this).scrollTop();

    if (offset >= tableOffset && $fixedHeader.is(":hidden")) {
        $fixedHeader.show();
    }
    else if (offset < tableOffset) {
        $fixedHeader.hide();
    }
});

工作示例:http://jsfiddle.net/fj8wM/9908/


0
投票

这是一个工作示例,您可以添加更多内容来检查它。

// When the user scrolls the page, execute myFunction
window.onscroll = function() {myFunction()};

// Get the header
var header = document.getElementById("myHeader");

// Get the offset position of the navbar
var sticky = header.offsetTop;

// Add the sticky class to the header when you reach its scroll position. Remove "sticky" when you leave the scroll position
function myFunction() {
  if (window.pageYOffset >= sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
/* Style the header */
.header {
  padding: 10px 16px;
  background: #555;
  color: #f1f1f1;
}

/* Page content */
.content {
  padding: 16px;
}

/* The sticky class is added to the header with JS when it reaches its scroll position */
.sticky {
  position: fixed;
  top: 0;
  width: 100%
}

/* Add some top padding to the page content to prevent sudden quick movement (as the header gets a new position at the top of the page (position:fixed and top:0) */
.sticky + .content {
  padding-top: 102px;
}
<table class="table">
    
        <tr class="header" id="myHeader">
            <th>Name</th>
            <th>Age</th>
        </tr>
    
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
    <tr>
        <td>
            someName
        </td>
        <td>
            someAge
        </td>
    </tr>
	 
</table>
© www.soinside.com 2019 - 2024. All rights reserved.