有没有 css3 方法来修复?在y轴上,让它在x轴上滚动?

发布于 2024-11-02 14:58:15 字数 988 浏览 0 评论 0原文

这个问题已经被问过无数次了:这里,<一个href="https://stackoverflow.com/questions/4334205/how-can-i-tell-iscroll-that-a-tbody-should-scroll-and-vanish-behind-a-thead"> 那里、 和 另一个地方就在所以。然而我找不到真正好的答案。

<回顾>通常我的表格在垂直方向上比视口深得多。我希望能够滚动表格的 ,同时其 保持固定且可见。其中一些表格也比视口宽得多。在这里,我希望表格的 水平滚动。

为了达到效果,我有几十行JS,包括setInterval()调用来检查scrollLeftscrollTop,以便我可以重新定位1副本。它确实有效,但它是一个巨大的、笨拙的、脆弱的、难以维持的痛苦。

问题:是否有一些简单的 css3 方式(现有的、新兴的或提议的),我可以用它来获取表的 < /code> 水平和垂直滚动,但彼此独立? 谢谢!


1 为什么setInterval( )?因为 IE 不统一传递 onScroll 事件,你傻了; 每个人都知道这一点!

This has been asked a zillion times: here, there, and the other place just on SO. Yet there's no real good answer that I can find.

<recap> Often I have tables that are vertically much deeper than the viewport. I'd like to be able to scroll the table's <tbody> while its <thead> remains fixed and visible. Some of these tables are also much wider than the viewport. Here I want the table's <thead> to scroll horizontally.

To get the effect, I have dozens of lines of JS, including setInterval( ) calls to check scrollLeft and scrollTop, so that I can reposition a copy of <thead>1. It works but it's a huge, ungainly, frail and unmaintainable pain in the ass.</recap>

Question: Is there some straightforward css3 way, existent or emerging or proposed, that I can use to get a a table's <thead> and <tbody> to scroll horizontally and vertically, yet independently of each other?
Thanks!


1 Why setInterval( )? Because IE doesn't uniformly deliver onScroll events, you silly; everybody knows that!

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

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

发布评论

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

评论(2

窗影残 2024-11-09 14:58:15

我最终根据@Naveed Ahmad 的建议对“解决方案”进行了建模。我将表的数据部分放在 中;并做了一个假的<头>我在 onLoad 时通过参考第一个表行中的 的宽度和偏移量填写,如下所示:

function position_col_heads ( ) {
  // get all the TD child elements from the first row
  var tds = main_tbody.children[0].getElementsByTagName('TD'); 
  for ( var i = 0; i < tds.length; ++i ) {
    thead.children[i].style.left =
      parseFloat( tds[i].offsetLeft ) + 'px';
    thead.children[i].style.width = 
      parseFloat( tds[i].scrollWidth ) + 'px';
  }
}

这或多或少可行 在此页面上

I ended up modeling the "solution" on the suggestion of @Naveed Ahmad. I put the data part of the table in a <tbody>; and made a fake <thead> that I filled in at onLoad time by referring to the widths and offsets of the <td>s in the first table row, like this:

function position_col_heads ( ) {
  // get all the TD child elements from the first row
  var tds = main_tbody.children[0].getElementsByTagName('TD'); 
  for ( var i = 0; i < tds.length; ++i ) {
    thead.children[i].style.left =
      parseFloat( tds[i].offsetLeft ) + 'px';
    thead.children[i].style.width = 
      parseFloat( tds[i].scrollWidth ) + 'px';
  }
}

This works more or less OK on this page.

铁轨上的流浪者 2024-11-09 14:58:15

一种肮脏的方法是将标题表放在单独的 div 中,例如:

<div class="header">
<table>
    <thead><tr><td>#</td><td>v</td></tr></thead>
</table>
</div>

然后将正文放在另一个 div 中,例如:

<div class="body">
    <table>
        <tbody>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>         
    </tbody>
</table> 
</div>

现在您可以为 body div 提供固定高度并设置 oveflow自动。就像:

table{border:0px solid #ccc;height:30px;}
table tr td{border:1px solid #ccc;padding:5px;}
div.body{height:70px;overflow:auto;border-bottom:1px solid #ccc;}

这是我在 jsfiddle 中所做的一个工作示例

a dirty way would be to put the header table in a separate div like:

<div class="header">
<table>
    <thead><tr><td>#</td><td>v</td></tr></thead>
</table>
</div>

Then body in the another div like:

<div class="body">
    <table>
        <tbody>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>
            <tr><td>1</td><td>a</td></tr>         
    </tbody>
</table> 
</div>

Now you can give a fixed height to body div and set oveflow to auto. like:

table{border:0px solid #ccc;height:30px;}
table tr td{border:1px solid #ccc;padding:5px;}
div.body{height:70px;overflow:auto;border-bottom:1px solid #ccc;}

here is a working example I did in jsfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文