React實(shí)現(xiàn)自動(dòng)滾動(dòng)表格的方法實(shí)例
在 React 中實(shí)現(xiàn)一個(gè)自動(dòng)滾動(dòng)的表格,可以通過 CSS 動(dòng)畫和 JavaScript 定時(shí)器來實(shí)現(xiàn)。以下是一個(gè)完整的示例代碼,包含示例數(shù)據(jù)和自動(dòng)滾動(dòng)功能。
實(shí)現(xiàn)思路:
- 自動(dòng)滾動(dòng):
使用 setInterval 實(shí)現(xiàn)表格的自動(dòng)滾動(dòng)。
- 手動(dòng)滾動(dòng):監(jiān)聽表格的 scroll 事件,當(dāng)用戶手動(dòng)滾動(dòng)時(shí),暫停自動(dòng)滾動(dòng)。
使用 setTimeout 檢測(cè)用戶是否停止?jié)L動(dòng),如果停止?jié)L動(dòng)一段時(shí)間(例如 2 秒),則恢復(fù)自動(dòng)滾動(dòng)。
- 鼠標(biāo)懸停
鼠標(biāo)懸停時(shí)停止?jié)L動(dòng),鼠標(biāo)移開后繼續(xù)滾動(dòng)
代碼
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); // 存儲(chǔ)自動(dòng)滾動(dòng)的定時(shí)器
// 開始自動(dòng)滾動(dòng)
const startAutoScroll = () => {
if (scrollIntervalRef.current) return; // 如果已經(jīng)有定時(shí)器,則不重復(fù)啟動(dòng)
scrollIntervalRef.current = setInterval(() => {
const table = tableRef.current;
if (!table) return;
const scrollHeight = table.scrollHeight; // 表格總高度
const clientHeight = table.clientHeight; // 可視區(qū)域高度
const maxScroll = scrollHeight - clientHeight; // 最大滾動(dòng)距離
let scrollTop = table.scrollTop + 1; // 每次滾動(dòng) 1px
if (scrollTop >= maxScroll) {
scrollTop = 0; // 滾動(dòng)到底部后回到頂部
}
table.scrollTop = scrollTop;
}, 50); // 每 50ms 滾動(dòng)一次
};
// 停止自動(dòng)滾動(dòng)
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;
// 初始化自動(dòng)滾動(dòng)
startAutoScroll();
// 監(jiān)聽鼠標(biāo)事件
table.addEventListener("mouseenter", handleMouseEnter);
table.addEventListener("mouseleave", handleMouseLeave);
// 清除事件監(jiān)聽和定時(shí)器
return () => {
table.removeEventListener("mouseenter", handleMouseEnter);
table.removeEventListener("mouseleave", handleMouseLeave);
stopAutoScroll();
};
}, []);
return (
<div className="table-container">
<h2>自動(dòng)滾動(dòng)表格(鼠標(biāo)懸停時(shí)停止?jié)L動(dòng))</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; /* 允許垂直滾動(dòng) */
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; /* 左對(duì)齊 */
}
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è)置表格容器的高度以實(shí)現(xiàn)滾動(dòng)效果 */
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實(shí)現(xiàn)自動(dòng)滾動(dòng)表格的兩種方法的文章就介紹到這了,更多相關(guān)React 自動(dòng)滾動(dòng)表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Next.js搭建Monorepo組件庫文檔實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Next.js搭建Monorepo組件庫文檔,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件
本篇文章主要介紹了react開發(fā)中如何使用require.ensure加載es6風(fēng)格的組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
React項(xiàng)目中服務(wù)器端渲染SSR的實(shí)現(xiàn)與優(yōu)化詳解
在傳統(tǒng)的 React 項(xiàng)目里,頁面的渲染工作是在瀏覽器里完成的,而服務(wù)器端渲染(SSR)則是讓服務(wù)器先把 React 組件渲染成 HTML 字符串,再把這個(gè) HTML 字符串發(fā)送給瀏覽器,下面我們就來看看具體實(shí)現(xiàn)方法吧2025-03-03
使用React手寫一個(gè)對(duì)話框或模態(tài)框的方法示例
這篇文章主要介紹了使用React手寫一個(gè)對(duì)話框或模態(tài)框的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
關(guān)于react hook useState連續(xù)更新對(duì)象的問題
這篇文章主要介紹了關(guān)于react hook useState連續(xù)更新對(duì)象的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
React?component.forceUpdate()強(qiáng)制重新渲染方式
這篇文章主要介紹了React?component.forceUpdate()強(qiáng)制重新渲染方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

