返回 jQuery 实例与特效代码列表
表格与数据MIT兼容 jQuery 1.12 / 3.8 / 4.1
表格固定表头
长表格纵向滚动时表头始终吸附顶部,滚动离开起点后自动增加阴影增强层次感。
兼容性说明:全版本兼容(含 slim 精简版)。
在线交互演示(独立沙箱)
$(function () {
var rows = [
['200', '请求成功'], ['201', '已创建'], ['204', '无内容'], ['301', '永久重定向'],
['302', '临时重定向'], ['304', '未修改'], ['400', '请求参数错误'], ['401', '未授权'],
['403', '禁止访问'], ['404', '资源未找到'], ['429', '请求过于频繁'], ['500', '服务器内部错误'], ['503', '服务暂不可用']
];
var html = rows.map(function (r, idx) {
return '<tr><td>' + (idx + 1) + '</td><td><strong>' + r[0] + '</strong></td><td>' + r[1] + '</td></tr>';
}).join('');
$('#sth-body').html(html);
$('#sth-box').on('scroll', function () {
$(this).toggleClass('scrolled', $(this).scrollTop() > 4);
});
});不用 jQuery 怎么写?(现代原生 JS / CSS 替代思路)
纯 CSS position: sticky; top: 0 即可完成表头吸顶,几乎不需要任何 JavaScript。
thead th { position: sticky; top: 0; background: #f8fafc; z-index: 2; }