js表格冻结列滚动条同步滚动

article/2025/10/11 13:52:01

用div css写的table表格怎么实现冻结某列,同时实现数据滚动条滚动的时候,表头也跟着滚动呢?

效果图:

代码:

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><style>.fixed-table-box {display: flex;margin-top: 20px;margin-left: 200px;width: 800px;flex-direction: column;flex-shrink: 0;flex-grow: 7;flex-direction: column;position: relative;}.fixed-table_fixed-left {display: flex;flex-shrink: 0;flex-direction: column;flex-grow: 1;width: 120px;position: absolute;top: 0;left: 0;z-index: 5;overflow: hidden;background: white;box-shadow: 4px 0px 8px -1px #d3d4d6;}.fixed-table_fixed-right {display: flex;flex-shrink: 0;flex-grow: 1;flex-direction: column;width: 132px;overflow: hidden;position: absolute;top: 0;right: 15px;z-index: 5;background: white;box-shadow: -4px 0px 8px -1px #d3d4d6;}.fixed-table-box_fixed-right-patch {width: 15px;height: 28px;border-top: 1px solid #4078b7;border-bottom: 1px solid #4078b7;background-color: #eef1f6;position: absolute;top: 0;right: 0;}.fixed-table_header {display: flex;height: 30px;line-height: 30px;width: 800px;flex-shrink: 0;overflow: hidden;}.fixed-table_header_th {display: flex;flex-grow: 1;border-top: 1px solid #4078b7;border-bottom: 1px solid #4078b7;background-color: #eef1f6;width: 120px;padding-left: 10px;flex-shrink: 0;}.column-scrollbar {width: 10px !important;}.fixed-table_body {display: flex;flex-direction: column;max-height: 197px;width: 800px;overflow: auto;}.fixed-table-left_body,.fixed-table-right_body {max-height: 180px;}.fixed-table_body_tr {display: flex;height: 30px;line-height: 30px;flex-shrink: 0;}.fixed-table_body_td {display: flex;flex-grow: 0;height: 30px;line-height: 30px;border-bottom: 1px solid #4078b7;width: 120px;flex-shrink: 0;padding-left: 10px;}.column-address {width: 240px;}</style>
</head><body><div class="fixed-table-box"><div id="fixed-table-main_header" class="fixed-table_header"><div class="fixed-table_header_th">日期</div><div class="fixed-table_header_th">姓名</div><div class="fixed-table_header_th">省份</div><div class="fixed-table_header_th">市区</div><div class="fixed-table_header_th column-address">地址</div><div class="fixed-table_header_th">邮编</div><div class="fixed-table_header_th">操作</div><div class="fixed-table_header_th column-scrollbar"></div></div><div onscroll="scroll(event)" class="fixed-table_body"><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div><div class="fixed-table_body_td">王小虎</div><div class="fixed-table_body_td">上海</div><div class="fixed-table_body_td">普陀区</div><div class="fixed-table_body_td column-address">上海市普陀区金沙江路 1518 路</div><div class="fixed-table_body_td">200333</div><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div></div><div class="fixed-table_fixed-left"><div class="fixed-table_header"><div class="fixed-table_header_th">日期</div></div><div class="fixed-table_body fixed-table-left_body"><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-03</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-04</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-05</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-06</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-07</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-08</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-09</div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td">2016-05-10</div></div></div></div><div class="fixed-table_fixed-right"><div class="fixed-table_header"><div class="fixed-table_header_th">操作</div></div><div class="fixed-table_body fixed-table-right_body"><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div><div class="fixed-table_body_tr"><div class="fixed-table_body_td"><a href="###">编辑</a>&nbsp;<a href="###">删除</a></div></div></div></div><div class="fixed-table-box_fixed-right-patch"></div></div><script type="text/javascript">//固定和滚动function scroll(event) {var mainHeader = document.querySelector("#fixed-table-main_header");var fixedLeft = document.querySelector(".fixed-table-left_body");var fixedRight = document.querySelector(".fixed-table-right_body");var mainTop = event.target.scrollTop;var mainLeft = event.target.scrollLeft;mainHeader.scrollLeft = mainLeft;fixedLeft.scrollTop = mainTop;console.log(mainTop);fixedRight.scrollTop = mainTop;}</script>
</body>
</html>

原理:

首先,冻结列是将要冻结的列单独做成一个table然后覆盖在原来的table上。

然后,同步滚动则就是通过οnscrοll="scroll(event)"方法,读取横向和纵向滚动距离,然后赋给表头和冻结列即可。


http://chatgpt.dhexx.cn/article/2xx9ExhX.shtml

相关文章

损失函数分类

损失函数 机器学习模型关于单个样本的预测值与真实值的差称为损失。损失越小&#xff0c;模型越好&#xff0c;如果预测值与真实值相等&#xff0c;就是没有损失。 损失函数&#xff08;Loss function&#xff09;是用来估量模型的预测值 f(x) 与真实值 Y 的不一致程度&#x…

损失函数总结

1. 概况 损失函数一般分为&#xff1a;0-1 损失函数&#xff0c;HingeLoss&#xff0c;绝对值损失函数&#xff0c;Huber Loss, 平方损失函数&#xff0c;对数损失函数&#xff0c;指数损失。 1. 0-1损失函数(zero-one loss) 0-1损失是指预测值和目标值不相等为1&#xff0…

matlab的损失函数mse,MSELoss损失函数

MSELoss损失函数中文名字就是&#xff1a;均方损失函数&#xff0c;公式如下所示&#xff1a; 这里 loss, x, y 的维度是一样的&#xff0c;可以是向量或者矩阵&#xff0c;i 是下标。 很多的 loss 函数都有 size_average 和 reduce 两个布尔类型的参数。因为一般损失函数都是直…

损失函数Loss Fuction

说说代价函数的作用&#xff1f; 1.为得到训练模型的参数&#xff0c;需要一个代价函数&#xff0c;通过训练代价函数来得到参数。    2.用于找到最优解的目的函数。 说说代价函数为什么要非负&#xff1f; 因为目标函数存在下界&#xff0c;在优化过程当中&#xff0c;如果优…

常用的损失函数

来自 机器学习成长之路公众号 本文将常用的损失函数分为了两大类&#xff1a;分类和回归。然后又分别对这两类进行了细分和讲解&#xff0c;其中回归中包含了一种不太常见的损失函数&#xff1a;平均偏差误差&#xff0c;可以用来确定模型中存在正偏差还是负偏差。 从学习任务…

损失函数设计

目录 1.常见损失函数 1.1 平方损失函数 1.2 绝对值损失函数 1.3 Huber损失函数 1.4 Hinge损失函数 1.5 交叉熵损失函数 1.6 指数损失函数 2.不对称损失函数设计 3.面向容错的损失函数设计 4.评测指标不可导时的损失函数设计 5.没有“Groud Truth“的损失函数设计 6…

如何选择合适的损失函数

【AI科技大本营导读】机器学习中的所有算法都依赖于最小化或最大化某一个函数&#xff0c;我们称之为“目标函数”。最小化的这组函数被称为“损失函数”。损失函数是衡量预测模型预测期望结果表现的指标。寻找函数最小值的最常用方法是“梯度下降”。把损失函数想象成起伏的山…

常用的损失函数合集

目录 一、什么是损失函数&#xff1f; 二、为什么需要损失函数&#xff1f; 三、损失函数通常使用的位置 四、损失函数的分类 五、常用的损失函数 1、回归损失&#xff08;针对连续型变量&#xff09; 1.L1 Loss也称为Mean Absolute Error&#xff0c;即平均绝对误差&…

语义分割的损失函数

语义分割任务实际上是一种像素层面上的分类&#xff0c;需要识别图像中存在的内容和位置&#xff0c;同样也存在与分类类似问题-样本类别不平衡&#xff0c;对于语义分割更多的是前景区域的样本远小于背景区域。针对类别不平衡问题&#xff0c;在loss层面上有不同的选择。 1. …

损失函数简介

损失函数的作用&#xff1a;衡量模型预测的好坏。 简单来说就是&#xff0c;损失函数就是用来表现预测与真实数据的差距程度。 令 真实值 为Y&#xff0c;预测值为 f(x)&#xff0c;损失函数为L( Y , f(x&#xff09;&#xff09;&#xff0c;关系如下&#xff1a; 损失函数&a…

Pyotorch自定义损失函数

&#x1f468;‍&#x1f4bb;作者简介&#xff1a;大数据专业硕士在读&#xff0c;CSDN人工智能领域博客专家&#xff0c;阿里云专家博主&#xff0c;专注大数据与人工智能知识分享&#xff0c;公众号&#xff1a;GoAI的学习小屋&#xff0c;免费分享书籍、简历、导图等资料&a…

GAN的损失函数

1.GAN 在训练过程中&#xff0c;生成器和判别器的目标是相矛盾的&#xff0c;并且这种矛盾可以体现在判别器的判断准确性上。生成器的目标是生成尽量真实的数据&#xff0c;最好能够以假乱真、让判别器判断不出来&#xff0c;因此生成器的学习目标是让判别器上的判断准确性越来…

损失函数比较

Pytorch学习之十九种损失函数 一、简介 损失函数&#xff08;Loss Function)是用来评估模型好坏程度&#xff0c;即预测值f(x)与真实值的不一致程度&#xff0c;通常表示为L(Y, f(x))的一个非负的浮点数。比如你要做一个线性回归&#xff0c;你拟合出来的曲线不会和原始的数据…

常见的损失函数

1、损失函数的意义 机器学习中的监督学习本质上是给定一系列训练样本 &#xff0c;尝试学习 的映射关系&#xff0c;使得给定一个 &#xff0c;即便这个 不在训练样本中&#xff0c;也能够得到尽量接近真实 的输出 。而损失函数&#xff08;Loss Function&#xff09;则是这个过…

损失函数

损失函数&#xff08;loss function&#xff09;是用来估量你模型的预测值f(x)与真实值Y的不一致程度&#xff0c;它是一个非负实值函数,通常使用L(Y, f(x))来表示&#xff0c;损失函数越小&#xff0c;模型的鲁棒性就越好。损失函数是经验风险函数的核心部分&#xff0c;也是结…

常见损失函数

常见损失函数 (cs231n) 文章目录 常见损失函数损失函数定义0-1损失函数(zero-one loss)绝对值损失函数(Absolute loss)log对数损失函数(Log Loss)平方损失函数(Square Loss)指数损失函数 (Exponential Loss)SVM损失函数&#xff08;Hinge Loss&#xff09;感知损失函数(percep…

【常见的损失函数总结】

文章目录 损失函数的定义神经网络的学习为何要设定损失函数&#xff1f;常见的损失函数1. 0-1损失函数(zero-one loss)2. 对数损失函数3. 平方损失函数MSE(均值平方差&#xff09;4. Hinge 损失函数5. 交叉熵损失函数 (Cross-entropy loss function) 损失函数的定义 损失函数&…

神经网络损失函数汇总

目录 一、基础知识 二、分类 &#xff08;1&#xff09;二分类交叉熵损失函数&#xff08;binary_crossentropy&#xff09; &#xff08;2&#xff09;categorical_crossentropy损失函数 &#xff08;3&#xff09;sparse_categorical_crossentropy &#xff08;4&#…

Jmeter 使用正则表达式提取器将返回值全部保存到一个文件中

目标&#xff1a;将响应数据 {"errorCode":0,"message":"success","data":null} 中的message字段的值全部保存到一个文件中。 1.正则表达式提取器 正则表达式&#xff1a;提取内容的正则表达式【()表示提取&#xff0c;对于你要提取的…