javascript - 如何模拟水平滚动的固定定位但保持正常的垂直滚动? (表格的粘性列)

标签 javascript jquery twitter-bootstrap css

首先让我说我无法相信我在这方面遇到了多少麻烦。我已经尝试了所有事情,但没有运气。不幸的是,我有很多要求需要满足,不能做出很多让步。

基本上,我需要制作一个可排序的表格,其第一列充当“粘性”列。所有其他列都可以水平滚动并“进入”第一列。

现在,这有点很简单。然而,第一列固定定位的问题是它不遵守它所在的 1200x600px 容器的边界。但更重要的是,应用固定定位使得第一列很难与其余列一起滚动正文。

一个例子:

enter image description here

现在,正如您想象的那样,当您滚动时,您会看到: enter image description here

从本质上讲,这首先违背了拥有粘性列的全部目的。

我想要做的就是让一切都完全按照现在的方式进行,而且还要将滚动条“绑定(bind)”到固定列上,以便它与表格的其余部分一起滚动。

但是,有几点需要考虑:

  • 表格的标题需要保持原样(即不能将所有内容都放入另一个 div 并滚动它。不确定这是否适用于固定定位)。
  • 所有内容都需要可排序,因此将两张表放在一起(正如我在其他答案中看到的那样)会使事情变得复杂,而 future 需要左右保持某种关联的努力。

我什至不能告诉你我多么希望这里有一些解决方案。我已经为此工作了一个星期。我已经尝试了所有的 JS 库,但它们的问题是这些库中的大多数都遵循创建多个表并在页面上隐藏一些表的约定。这在滚动时引入了很多延迟(尤其是在有大量数据的情况下)。如果我们能以某种方式找到一种方法以最少的黑客攻击来做到这一点,那将是理想的,尽管在这一点上我会尝试任何事情。

好消息是我们始终可以依靠每个表格单元格的固定高度。

提前致谢!

最佳答案

您好 ActiveModel 第一个表格列在下面的页面中是粘性的

<!DOCTYPE HTML>
<html>
<head>
    <style>
        .table-scroll {
            position: relative;
            max-width: 600px;
            margin: auto;
            overflow: hidden;
            border: 1px solid #000;
        }

        .table-wrap {
            width: 100%;
            overflow: auto;
        }

        .table-scroll table {
            width: 100%;
            margin: auto;
            border-collapse: separate;
            border-spacing: 0;
        }

        .table-scroll th, .table-scroll td {
            padding: 5px 10px;
            border: 1px solid #000;
            background: #fff;
            white-space: nowrap;
            vertical-align: top;
        }

        .table-scroll thead, .table-scroll tfoot {
            background: #f9f9f9;
        }

        .clone {
            position: absolute;
            top: 0;
            left: 0;
            pointer-events: none;
        }

            .clone th, .clone td {
                visibility: hidden
            }

            .clone td, .clone th {
                border-color: transparent
            }

            .clone tbody th {
                visibility: visible;
                color: red;
            }

            .clone .fixed-side {
                border: 1px solid #000;
                background: #eee;
                visibility: visible;
            }

            .clone thead, .clone tfoot {
                background: transparent;
            }
    </style>
 </head >

<body>
    <div id="table-scroll" class="table-scroll">
        <div class="table-wrap">
            <table class="main-table">
                <thead>
                    <tr>
                        <th class="fixed-side" scope="col">&nbsp;</th>
                        <th scope="col">Header 2</th>
                        <th scope="col">Header 3</th>
                        <th scope="col">Header 4</th>
                        <th scope="col">Header 5</th>
                        <th scope="col">Header 6</th>
                        <th scope="col">Header 7</th>
                        <th scope="col">Header 8</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>
                            Cell content<br>
                            test
                        </td>
                        <td><a href="#">Cell content longer</a></td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>Cell content</td>
                        <td>Cell content longer</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>Cell content</td>
                        <td>Cell content longer</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>Cell content</td>
                        <td>Cell content longer</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>Cell content</td>
                        <td>Cell content longer</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                    <tr>
                        <th class="fixed-side">Left Column</th>
                        <td>Cell content</td>
                        <td>Cell content longer</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                        <td>Cell content</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th class="fixed-side">&nbsp;</th>
                        <td>Footer 2</td>
                        <td>Footer 3</td>
                        <td>Footer 4</td>
                        <td>Footer 5</td>
                        <td>Footer 6</td>
                        <td>Footer 7</td>
                        <td>Footer 8</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <p>See <a href="https://codepen.io/paulobrien/pen/LBrMxa" target="blank">position Sticky version </a>with no JS</p>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <script>
       jQuery(document).ready(function () {
           jQuery(".main-table").clone(true).appendTo('#table-scroll').addClass('clone');
       });
   </script>
</body>
 </html >

关于javascript - 如何模拟水平滚动的固定定位但保持正常的垂直滚动? (表格的粘性列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712199/

相关文章:

javascript - 是否可以访问 javascript 类层次结构中的所有级别?

javascript - 如何从 Kendo View 模型获取 DOM 元素?

javascript - IE td 元素的 innerhtml 解决方法?

jquery - ASP.NET MVC3 : Datetimepicker wont work

javascript - 当通过 MVC 局部 View 注入(inject)时,Bootstrap 模态显示和隐藏事件不会触发

javascript - 在 Javascript 中将对象原型(prototype)函数绑定(bind)到对象

jquery - 如何在此脚本中添加 cookie 来缓存样式表更改

javascript - 开始向下滚动后更改菜单背景不透明度

twitter-bootstrap - Bootstrap 3.0.0 glyphicon-耳机未加载

javascript - Bootstrap 模式是否显示取决于 bootstrap 版本