带有固定标题的仅 CSS 可滚动表

本文介绍了带有固定标题的仅 CSS 可滚动表的处理方法,对大家解决问题具有一定的参考价值

问题描述

我有一个解决方案,通过它我可以使用次要的 jQuery 和 CSS 创建带有固定页眉/页脚的可滚动表格 - 但我正在寻找一种方法来使其成为跨浏览器兼容的纯 CSS 解决方案.

明确地说,我想要做的是只使用一个table标签(它是有效的子标签,colgroupcoltheadtbodytfoottrth, td),但采用一组 CSS 规则,将满足以下条件:

  1. 必须保持页眉/页脚/内容行之间的列对齐
  2. 必须允许页眉/页脚在内容垂直滚动时保持固定
  3. 不得需要任何 jQuery 或其他 JavaScript 才能提供功能
  4. 只能使用上面提供的标签

此代码示例:http://jsfiddle.net/TroyAlford/SNKfd/ 显示了我目前的方法.大多数 JS 只是用随机值填充表格,但最后一部分是驱动左右滚动的.

$tbody.bind('scroll', function(ev) {var $css = { 'left': -ev.target.scrollLeft };$thead.css($css);$tfoot.css($css);});

注意:提供的示例在 IE 中无法正确呈现,需要 jQuery 提供水平滚动.反正我不关心水平滚动,所以如果解决方案不这样做也没关系.

解决方案

此答案将用作不完全支持的 position:sticky 的占位符,并将随时间更新.目前建议不要在生产环境中使用本机实现.

查看当前支持:https://caniuse.com/#feat=css-sticky


使用position:sticky

另一种答案是使用position:sticky.如 W3C 所述:

<块引用>

粘性定位框的定位与相对定位框类似,但偏移量是根据最近的具有滚动框的祖先或视口(如果没有祖先具有滚动框)计算的.

这准确地描述了相对静态标头的行为.将它分配给 或第一个 HTML 标签会很容易,因为这应该被支持 根据 W3C.但是,ChromeIE 和 Edge 在为这些标签分配粘性位置属性时遇到问题.目前似乎也没有优先解决这个问题.

似乎对表格元素有用的是将粘性属性分配给表格单元格.在这种情况下, 单元格.

因为表格不是尊重您分配给它的静态大小的块元素,所以最好使用包装元素来定义滚动溢出.

代码

div {显示:内联块;高度:150px;溢出:自动}表 th {位置:-webkit-sticky;位置:粘性;顶部:0;}/* == 只是一般样式,不相关:) == */桌子 {边框折叠:折叠;}第 {背景色:#1976D2;颜色:#fff;},TD{填充:1em .5em;}表 tr {颜色:#212121;}表 tr:nth-child(odd) {背景颜色:#BBDEFB;}

<表格边框=0"><头><tr><th>head1</th><th>head2</th><th>head3</th><th>head4</th></tr></thead><tr><td>第1行,单元格1</td><td>第1行,单元格2</td><td>第1行,单元格2</td><td>第1行,单元格2</td></tr><tr><td>第2行,单元格1</td><td>第2行,单元格2</td><td>第1行,单元格2</td><td>第1行,单元格2</td></tr><tr><td>第2行,单元格1</td><td>第2行,单元格2</td><td>第1行,单元格2</td><td>第1行,单元格2</td></tr><tr><td>第2行,单元格1</td><td>第2行,单元格2</td><td>第1行,单元格2</td><td>第1行,单元格2</td></tr><tr><td>第2行,单元格1</td><td>第2行,单元格2</td><td>第1行,单元格2</td><td>第1行,单元格2</td></tr>

在这个例子中,我使用一个简单的

包装器来定义使用 150px 的静态高度完成的滚动溢出.这当然可以是任何大小.现在已经定义了滚动框,粘性 元素将对应到最近的带有滚动框的祖先",即 div-wrapper.


使用position:sticky polyfill

不受支持的设备可以使用 polyfill,它通过代码实现行为.一个例子是 stickybits,它与浏览器实现的 position:sticky.

polyfill 示例: http://jsfiddle.net/7UZA4/6957/

I have a solution by which I can create scrollable tables w/fixed header/footer using minor jQuery and CSS - but I am looking for a way to make this a CSS-only solution that is cross-browser compliant.

To be clear, what I am seeking to do is use only a table tag (and it's valid sub-tags, colgroup, col, thead, tbody, tfoot, tr, th, td), but adopt a set of CSS rules which will meet the following conditions:

  1. Must maintain column alignment between header / footer / content rows
  2. Must allow the header/footer to remain fixed while the content scrolls vertically
  3. Must not require any jQuery or other JavaScript in order to provide the functionality
  4. Must only use the tags provided above

This code example: http://jsfiddle.net/TroyAlford/SNKfd/ shows my current approach. Most of the JS is just to populate the table with random values, but the last portion is what drives the left/right scrollability.

$tbody.bind('scroll', function(ev) {
    var $css = { 'left': -ev.target.scrollLeft };
    $thead.css($css);
    $tfoot.css($css);
});

NOTE: The example provided does not render properly in IE, and requires jQuery to provide the horizontal scrolling. I don't care about horizontal scrolling anyway, so it's fine if a solution doesn't do that.

解决方案

This answer will be used as a placeholder for the not fully supported position: sticky and will be updated over time. It is currently advised to not use the native implementation of this in a production environment.

See this for the current support: https://caniuse.com/#feat=css-sticky


Use of position: sticky

An alternative answer would be using position: sticky. As described by W3C:

A stickily positioned box is positioned similarly to a relatively positioned box, but the offset is computed with reference to the nearest ancestor with a scrolling box, or the viewport if no ancestor has a scrolling box.

This described exactly the behavior of a relative static header. It would be easy to assign this to the <thead> or the first <tr> HTML-tag, as this should be supported according to W3C. However, both Chrome, IE and Edge have problems assigning a sticky position property to these tags. There also seems to be no priority in solving this at the moment.

What does seem to work for a table element is assigning the sticky property to a table-cell. In this case the <th> cells.

Because a table is not a block-element that respects the static size you assign to it, it is best to use a wrapper element to define the scroll-overflow.

The code

div {
  display: inline-block;
  height: 150px;
  overflow: auto
}

table th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}


/* == Just general styling, not relevant :) == */

table {
  border-collapse: collapse;
}

th {
  background-color: #1976D2;
  color: #fff;
}

th,
td {
  padding: 1em .5em;
}

table tr {
  color: #212121;
}

table tr:nth-child(odd) {
  background-color: #BBDEFB;
}

<div>
  <table border="0">
    <thead>
      <tr>
        <th>head1</th>
        <th>head2</th>
        <th>head3</th>
        <th>head4</th>
      </tr>
    </thead>
    <tr>
      <td>row 1, cell 1</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
    </tr>
    <tr>
      <td>row 2, cell 1</td>
      <td>row 2, cell 2</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
    </tr>
    <tr>
      <td>row 2, cell 1</td>
      <td>row 2, cell 2</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
    </tr>
    <tr>
      <td>row 2, cell 1</td>
      <td>row 2, cell 2</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
    </tr>
    <tr>
      <td>row 2, cell 1</td>
      <td>row 2, cell 2</td>
      <td>row 1, cell 2</td>
      <td>row 1, cell 2</td>
    </tr>
  </table>
</div>

In this example I use a simple <div> wrapper to define the scroll-overflow done with a static height of 150px. This can of course be any size. Now that the scrolling box has been defined, the sticky <th> elements will corespondent "to the nearest ancestor with a scrolling box", which is the div-wrapper.


Use of a position: sticky polyfill

Non-supported devices can make use of a polyfill, which implements the behavior through code. An example is stickybits, which resembles the same behavior as the browser's implemented position: sticky.

Example with polyfill: http://jsfiddle.net/7UZA4/6957/

这篇关于带有固定标题的仅 CSS 可滚动表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

查看全文

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1159

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1060

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:799

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:885

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:871

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:854

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1690

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1296