React實現(xiàn)自動滾動表格的方法實例
在 React 中實現(xiàn)一個自動滾動的表格,可以通過 CSS 動畫和 JavaScript 定時器來實現(xiàn)。以下是一個完整的示例代碼,包含示例數(shù)據(jù)和自動滾動功能。
實現(xiàn)思路:
- 自動滾動:
使用 setInterval 實現(xiàn)表格的自動滾動。
- 手動滾動:監(jiān)聽表格的 scroll 事件,當(dāng)用戶手動滾動時,暫停自動滾動。
使用 setTimeout 檢測用戶是否停止?jié)L動,如果停止?jié)L動一段時間(例如 2 秒),則恢復(fù)自動滾動。
- 鼠標(biāo)懸停
鼠標(biāo)懸停時停止?jié)L動,鼠標(biāo)移開后繼續(xù)滾動
代碼
import React, { useState, useEffect, useRef } from "react";
import "../Styles/AutoScrollTable.css"; // 引入樣式文件
const AutoScrollTable = () => {
// 示例數(shù)據(jù)
const [data, setData] = useState([
{ id: 1, name: "Alice", age: 25, city: "New York" },
{ id: 2, name: "Bob", age: 30, city: "Los Angeles" },
{ id: 3, name: "Charlie", age: 35, city: "Chicago" },
{ id: 4, name: "David", age: 40, city: "Houston" },
{ id: 5, name: "Eva", age: 28, city: "Phoenix" },
{ id: 6, name: "Frank", age: 33, city: "Philadelphia" },
{ id: 7, name: "Grace", age: 22, city: "San Antonio" },
{ id: 8, name: "Hank", age: 45, city: "San Diego" },
{ id: 9, name: "Ivy", age: 29, city: "Dallas" },
{ id: 10, name: "Jack", age: 31, city: "San Jose" },
]);
const tableRef = useRef(null); // 用于引用表格容器
const scrollIntervalRef = useRef(null); // 存儲自動滾動的定時器
// 開始自動滾動
const startAutoScroll = () => {
if (scrollIntervalRef.current) return; // 如果已經(jīng)有定時器,則不重復(fù)啟動
scrollIntervalRef.current = setInterval(() => {
const table = tableRef.current;
if (!table) return;
const scrollHeight = table.scrollHeight; // 表格總高度
const clientHeight = table.clientHeight; // 可視區(qū)域高度
const maxScroll = scrollHeight - clientHeight; // 最大滾動距離
let scrollTop = table.scrollTop + 1; // 每次滾動 1px
if (scrollTop >= maxScroll) {
scrollTop = 0; // 滾動到底部后回到頂部
}
table.scrollTop = scrollTop;
}, 50); // 每 50ms 滾動一次
};
// 停止自動滾動
const stopAutoScroll = () => {
if (scrollIntervalRef.current) {
clearInterval(scrollIntervalRef.current);
scrollIntervalRef.current = null;
}
};
// 處理鼠標(biāo)事件
const handleMouseEnter = () => {
stopAutoScroll();
};
const handleMouseLeave = () => {
startAutoScroll();
};
useEffect(() => {
const table = tableRef.current;
if (!table) return;
// 初始化自動滾動
startAutoScroll();
// 監(jiān)聽鼠標(biāo)事件
table.addEventListener("mouseenter", handleMouseEnter);
table.addEventListener("mouseleave", handleMouseLeave);
// 清除事件監(jiān)聽和定時器
return () => {
table.removeEventListener("mouseenter", handleMouseEnter);
table.removeEventListener("mouseleave", handleMouseLeave);
stopAutoScroll();
};
}, []);
return (
<div className="table-container">
<h2>自動滾動表格(鼠標(biāo)懸停時停止?jié)L動)</h2>
<div className="scrollable-table" ref={tableRef}>
<table style={{ borderCollapse: 'collapse', width: '100%', border: '1px solid black' }}>
<thead>
<tr>
<th style={{ border: '1px solid black', padding: '8px' }}>ID</th>
<th style={{ border: '1px solid black', padding: '8px' }}>Name</th>
<th style={{ border: '1px solid black', padding: '8px' }}>Age</th>
<th style={{ border: '1px solid black', padding: '8px' }}>City</th>
</tr>
</thead>
<tbody>
{data.map((item) => (
<tr key={item.id} style={{ borderBottom: '1px solid black' }}>
<td style={{ border: '1px solid black', padding: '8px' }}>{item.id}</td>
<td style={{ border: '1px solid black', padding: '8px' }}>{item.name}</td>
<td style={{ border: '1px solid black', padding: '8px' }}>{item.age}</td>
<td style={{ border: '1px solid black', padding: '8px' }}>{item.city}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
};
export default AutoScrollTable;
樣式文件 (AutoScrollTable.css):
.table-container {
margin: 20px;
font-family: Arial, sans-serif;
}
.scrollable-table {
max-height: 300px; /* 設(shè)置表格的最大高度 */
overflow-y: auto; /* 允許垂直滾動 */
border: 1px solid #ccc; /* 表格邊框 */
border-radius: 4px; /* 圓角 */
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); /* 陰影效果 */
}
table {
width: 100%;
border-collapse: collapse; /* 單元格合并邊框 */
}
th, td {
border: 1px solid #ddd; /* 單元格邊框 */
padding: 12px; /* 單元格內(nèi)邊距 */
text-align: left; /* 左對齊 */
}
th {
background-color: #f4f4f4; /* 表頭背景顏色 */
font-weight: bold; /* 加粗字體 */
}
tr:nth-child(even) {
background-color: #f9f9f9; /* 偶數(shù)行背景色 */
}
tr:hover {
background-color: #f1f1f1; /* 鼠標(biāo)懸停行的背景色 */
}
.table-container {
width: 95%;
padding: 20px;
box-sizing: border-box;
}
.scrollable-table {
overflow: auto;
height: 300px; /* 設(shè)置表格容器的高度以實現(xiàn)滾動效果 */
border: 1px solid #ccc;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
}
thead {
position: sticky;
top: 0;
background-color: #f8f8f8;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #5179be;
color: white;
}
tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
tbody tr:hover {
background-color: #ddd;
}
效果圖:

到此這篇關(guān)于React實現(xiàn)自動滾動表格的兩種方法的文章就介紹到這了,更多相關(guān)React 自動滾動表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ant Design與Ant Design pro入門使用教程
Ant Design 是一個服務(wù)于企業(yè)級產(chǎn)品的設(shè)計體系,組件庫是它的 React 實現(xiàn),antd 被發(fā)布為一個 npm 包方便開發(fā)者安裝并使用,這篇文章主要介紹了Ant Design與Ant Design pro入門,需要的朋友可以參考下2023-12-12
React Scheduler 最小堆實現(xiàn)小結(jié)
本文主要介紹了React Scheduler 最小堆實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
react 項目 中使用 Dllplugin 打包優(yōu)化技巧
在用 Webpack 打包的時候,對于一些不經(jīng)常更新的第三方庫,比如 react,lodash,vue 我們希望能和自己的代碼分離開,這篇文章主要介紹了react 項目 中 使用 Dllplugin 打包優(yōu)化,需要的朋友可以參考下2023-01-01
React-Native使用Mobx實現(xiàn)購物車功能
本篇文章主要介紹了React-Native使用Mobx實現(xiàn)購物車功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
React中Ant?Design組件日期編輯回顯的實現(xiàn)
本文主要介紹了React中Ant?Design組件日期編輯回顯的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
React.memo?React.useMemo對項目性能優(yōu)化使用詳解
這篇文章主要為大家介紹了React.memo?React.useMemo對項目性能優(yōu)化的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
修復(fù)Next.js中window?is?not?defined方法詳解
這篇文章主要為大家介紹了修復(fù)Next.js中window?is?not?defined方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

