html +css 实现一个表头固定表体自动循环无缝滚动的表格,鼠标悬浮时停止,滚动出去的数据自动插入后边

时间: 2023-12-03 11:42:36 浏览: 185
实现表头固定表体自动循环无缝滚动的表格,可以使用以下步骤: 1. HTML 结构 首先,我们需要先搭建表格的 HTML 结构。代码如下: ```html <div class="table-wrap"> <table> <thead> <tr> <th>表头1</th> <th>表头2</th> <th>表头3</th> <th>表头4</th> <th>表头5</th> </tr> </thead> <tbody> <tr> <td>数据1-1</td> <td>数据1-2</td> <td>数据1-3</td> <td>数据1-4</td> <td>数据1-5</td> </tr> <tr> <td>数据2-1</td> <td>数据2-2</td> <td>数据2-3</td> <td>数据2-4</td> <td>数据2-5</td> </tr> <!-- 其他数据行 --> </tbody> </table> </div> ``` 2. CSS 样式 接下来,我们需要设置表格的 CSS 样式,包括表格的宽度、高度、边框等属性,以及表头的固定和表体的滚动效果。代码如下: ```css .table-wrap { width: 100%; height: 300px; overflow: hidden; } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ccc; padding: 8px; text-align: center; } thead { position: sticky; top: 0; background-color: #fff; } tbody { animation: scroll 20s linear infinite; } @keyframes scroll { 0% { transform: translateY(0); } 100% { transform: translateY(-100%); } } ``` 其中,`.table-wrap` 是表格容器的类名,设置了容器的宽度、高度和溢出隐藏;`table` 是表格的标签选择器,设置了表格的宽度和边框合并;`th` 和 `td` 分别是表头和数据行的样式,设置了单元格的边框、内边距和居中对齐;`thead` 是表头的选择器,设置了表头的固定和背景颜色;`tbody` 是表体的选择器,设置了表体的滚动效果和动画时间。 3. JavaScript 交互 最后,我们需要使用 JavaScript 实现鼠标悬浮停止滚动和滚动出去的数据自动插入的效果。代码如下: ```javascript const tableWrap = document.querySelector('.table-wrap') let timer = null tableWrap.addEventListener('mouseover', () => { clearInterval(timer) }) tableWrap.addEventListener('mouseout', () => { timer = setInterval(() => { const firstRow = tableWrap.querySelector('tbody tr:first-child') const cloneRow = firstRow.cloneNode(true) tableWrap.querySelector('tbody').appendChild(cloneRow) firstRow.remove() }, 2000) }) timer = setInterval(() => { const firstRow = tableWrap.querySelector('tbody tr:first-child') const cloneRow = firstRow.cloneNode(true) tableWrap.querySelector('tbody').appendChild(cloneRow) firstRow.remove() }, 2000) ``` 其中,`tableWrap` 是表格容器的 DOM 元素,使用 `querySelector` 方法获取;`timer` 是定时器变量;`mouseover` 和 `mouseout` 是鼠标悬浮和移出容器的事件,分别清除和启动定时器;定时器中的代码实现了滚动出去的数据自动插入后边的效果,具体实现方法是先使用 `querySelector` 获取表格数据的第一行元素 `firstRow`,然后使用 `cloneNode` 方法克隆这一行元素,再使用 `appendChild` 方法将克隆的行元素添加到表格数据的最后一行,最后使用 `remove` 方法删除原来的第一行元素。 综上所述,HTML、CSS 和 JavaScript 的代码实现如下: ```html <div class="table-wrap"> <table> <thead> <tr> <th>表头1</th> <th>表头2</th> <th>表头3</th> <th>表头4</th> <th>表头5</th> </tr> </thead> <tbody> <tr> <td>数据1-1</td> <td>数据1-2</td> <td>数据1-3</td> <td>数据1-4</td> <td>数据1-5</td> </tr> <tr> <td>数据2-1</td> <td>数据2-2</td> <td>数据2-3</td> <td>数据2-4</td> <td>数据2-5</td> </tr> <!-- 其他数据行 --> </tbody> </table> </div> <style> .table-wrap { width: 100%; height: 300px; overflow: hidden; } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #ccc; padding: 8px; text-align: center; } thead { position: sticky; top: 0; background-color: #fff; } tbody { animation: scroll 20s linear infinite; } @keyframes scroll { 0% { transform: translateY(0); } 100% { transform: translateY(-100%); } } </style> <script> const tableWrap = document.querySelector('.table-wrap') let timer = null tableWrap.addEventListener('mouseover', () => { clearInterval(timer) }) tableWrap.addEventListener('mouseout', () => { timer = setInterval(() => { const firstRow = tableWrap.querySelector('tbody tr:first-child') const cloneRow = firstRow.cloneNode(true) tableWrap.querySelector('tbody').appendChild(cloneRow) firstRow.remove() }, 2000) }) timer = setInterval(() => { const firstRow = tableWrap.querySelector('tbody tr:first-child') const cloneRow = firstRow.cloneNode(true) tableWrap.querySelector('tbody').appendChild(cloneRow) firstRow.remove() }, 2000) </script> ```

相关推荐

最新推荐

recommend-type

利用纯css实现table固定列与表头中间横向滚动的思路和实例

主要给大家介绍了关于如何利用纯css实现table固定列与表头,中间横向滚动的相关资料,文中通过示例代码详细的给大家介绍了关于实现该效果的思路与方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们...
recommend-type

解决layui表格的表头不滚动的问题

今天小编就为大家分享一篇解决layui表格的表头不滚动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

利用js+css+html实现固定table的列头不动

话不多说,跟这小编来一起看下吧 ... //该函数在上面一个table数据加载完成后调用 //把表头的宽度设置到会滚动的页头去 var copyWidth = function () { var b = $('#data_tbody').prev().find('tr:last').fi
recommend-type

html+css实现数据图表的展示效果

一下收集分享的数据图表的展示效果,是完全用html和css来实现的。通过仔细研究学习这些案例对于我们学习css会有很大帮助的。
recommend-type

Vue多种方法实现表头和首列固定的示例代码

有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定, 上效果: 一、创建多个表格进行覆盖 思路:当页面滚动到临界值时,出现固定表头、首列 先创建一个活动表格 &lt;!DOCTYPE html&gt; &...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。