CSS:固定标头表 - 滚动时,如何不专注于固定标头后面的视线外行?

发布于 2025-02-07 04:15:22 字数 493 浏览 0 评论 0原文

背景

I有一个带固定标头的表(确切地说是固定< th>),带有垂直滚动内容。您只需像往常一样在滚动条上上下滚动,就可以使用上下箭头键上下滚动。我强调了当前专注的行带有红色轮廓的地方。在下面亲自查看。

问题

问题是,当向下滚动并尝试使用up箭头键时,它可以到达固定标头下方的第一行。顶行不可见,因此,当使用UP键滚动时,请重点关注这一点。

问题

如何防止专注于“视图”行?理想情况下,我希望它是否达到最后一个可见的行,而不是固定标头后面的一排,它可以进一步向上滚动。也许我应该用标头做点什么?

最小可重复的代码

在这里是 jsfiddle代码

Background

I have a table with fixed header (fixed <th>s to be exact), with vertically scrollable content. You just scroll up and down with the scroll bar as usual, and you can scroll up and down with up/down arrow keys. I am highlighting where is the currently focused row with a red outline. See for yourself below.

Problem

The problem is when scroll down a bit and try to go up with up arrow key, it reaches the top row under the fixed header. the top row is not visible, so focusing on that when scrolling up with up key is not favorable.

Question

How do I prevent focusing on a "out-of-view" row? Ideally I want it to scroll further up if it reaches the top last visible row, not the one behind the fixed header. Maybe I should do something with the header?

Minimum reproducible code

Here is the JSFiddle code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2025-02-14 04:15:22

您可以将container.scrolltop = 0设置为编程滚动至非常顶。适应您案件的代码:

    case 38: // up
        currentRow?.previousElementSibling?.focus();
        // check if we've reached the first <tr />
        // if so, set container.scrollTop = 0
        if (!currentRow?.previousElementSibling?.previousElementSibling) {
            e.target.closest(".table").scrollTop = 0;
        }
        break;

You can just set container.scrollTop = 0 to programmatically scroll to very top. Code adapted to your case:

    case 38: // up
        currentRow?.previousElementSibling?.focus();
        // check if we've reached the first <tr />
        // if so, set container.scrollTop = 0
        if (!currentRow?.previousElementSibling?.previousElementSibling) {
            e.target.closest(".table").scrollTop = 0;
        }
        break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文