简体   繁体   English

如何使单行在HTML中不可滚动

[英]How to make single row Non scrollable in HTML

I want to create a table scrollable but i need to freeze first row of the table from scrolling. 我想创建一个可滚动的表,但我需要冻结滚动表的第一行。 how could i achieve this. 我怎么能做到这一点。 Expecting any ideas from CSS or JS or Jquery 期待来自CSS或JS或Jquery的任何想法

Your requirement is a very common use case, and quite sadly there is no native way of achieving it in any browser. 您的需求是一个非常常见的用例,非常可惜的是,在任何浏览器中都没有原生的方法来实现它。 You have plenty workarounds using CSS, JS, combinations, each solution having specific advantages and inconvenients (the main most common one being the need to fix the columns widths). 使用CSS,JS,组合有很多解决方法,每种解决方案都有其特定的优点和不便之处(最常见的一种解决方案是固定列宽)。

You can of course implement it yourself (browse on the web, you should have plenty tutorials for this). 您当然可以自己实现它(在Web上浏览,为此您应该有很多教程)。 Or you can rely on a library that avoids common pitfalls and is most cross-browsers. 或者,您可以依靠一个避免常见陷阱并且是大多数跨浏览器的库。

You would probably be interested in DataTables plugin for jQuery, and especially its FixedHeader extension. 您可能会对jQuery的DataTables插件特别是其FixedHeader扩展感兴趣

example. 例。 HTML 的HTML

<table class="scroll">
    <thead>
        <tr>
            <th>Head 1</th>
            <th>Head 2</th>
            <th>Head 3</th>
            <th>Head 4</th>
            <th>Head 5</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td>5</td>
        </tr>
        <tr>
         <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td>5</td>
        </tr>
        <tr>
           <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td>5</td>
        </tr>
        <tr>
            <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td>5</td>
        </tr>
        <tr>
            <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td>4</td>
            <td> 5</td>
        </tr>
        <tr>
            <td> 1</td>
            <td> 2</td>
            <td> 3</td>
            <td> 4</td>
            <td> 5</td>
        </tr>
    </tbody>
</table>

css 的CSS

table.scroll {
    /* width: 100%; */ /* Optional */
    /* border-collapse: collapse; */
    border-spacing: 0;
    border: 2px solid black;
}

table.scroll tbody,
table.scroll thead { display: block; }

thead tr th { 
    height: 30px;
    line-height: 30px;
    /* text-align: left; */
}

table.scroll tbody {
    height: 100px;
    overflow-y: auto;
    overflow-x: hidden;
}

tbody { border-top: 2px solid black; }

tbody td, thead th {
    /* width: 20%; */ /* Optional */
    border-right: 1px solid black;
    /* white-space: nowrap; */
}

tbody td:last-child, thead th:last-child {
    border-right: none;
}

JS JS

// Change the selector if needed
var $table = $('table.scroll'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

// Adjust the width of thead cells when window resizes
$(window).resize(function() {
    // Get the tbody columns width array
    colWidth = $bodyCells.map(function() {
        return $(this).width();
    }).get();

    // Set the width of thead columns
    $table.find('thead tr').children().each(function(i, v) {
        $(v).width(colWidth[i]);
    });    
}).resize(); // Trigger resize handler

Using Angular you could use the FixedHeader module like this found on ngmodules http://ngmodules.org/modules/FixedHeader . 采用了棱角分明您可以使用FixedHeader模块像这样的ngmodules发现http://ngmodules.org/modules/FixedHeader Or using jquery Plug in http://markmalek.github.io/Fixed-Header-Table/ 或使用jquery插入http://markmalek.github.io/Fixed-Header-Table/

Also check out using css position: sticky http://charliepark.org/css-only-sticky-headers/ 还要使用CSS位置签出:粘性http://charliepark.org/css-only-sticky-headers/

This is called Table Fixed Header Scrolling. 这称为表固定标题滚动。 You can use this jquery plugin it's very simple to be implemented fixedheadertable plugin 您可以使用此jquery插件,实现起来非常简单fixedheadertable插件

使用插件fixedheadertableDatatable参考链接Stack over Flow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM