首页 > 其他分享 >React 虚拟滚动 长列表

React 虚拟滚动 长列表

时间:2023-09-20 19:56:17浏览次数:68  
标签:nextIndex const list 列表 React startIndex visibleNum fixedHeight 滚动

定高版本

 1 "use client";
 2 import React, { useCallback, useMemo, useState } from "react";
 3 
 4 interface IProps {
 5   list: any[];
 6   fixedHeight: number;
 7 }
 8 
 9 // Fixed height
10 const VirtualView = (props: IProps) => {
11   const { list, fixedHeight } = props;
12 
13   const [startIndex, setStarIndex] = useState(0);
14   const [endIndex, setEndIndex] = useState(10);
15 
16   const targetList = list.slice(startIndex, endIndex);
17   const visibleNum = targetList.length;
18 
19   const { containerHeight, viewHeight } = useMemo(() => {
20     return {
21       viewHeight: visibleNum * fixedHeight,
22       containerHeight: list.length * fixedHeight,
23     };
24   }, [list, fixedHeight, visibleNum]);
25   console.log(startIndex, endIndex, visibleNum);
26 
27   const maxIndex = list.length - visibleNum;
28   const handleScroll = useCallback(
29     (e: any) => {
30       const scrollTop = e?.target?.scrollTop as number;
31       let nextIndex = Math.floor(scrollTop / fixedHeight);
32       nextIndex = Math.min(maxIndex, nextIndex);
33       setStarIndex(nextIndex);
34       setEndIndex(visibleNum + nextIndex);
35     },
36     [fixedHeight, setStarIndex, setEndIndex, maxIndex, visibleNum]
37   );
38   return (
39     <div
40       onScroll={handleScroll}
41       className={`virtual-list-view relative w-1/2 overflow-y-auto border-2 border-l-indigo-800`}
42       style={{ maxHeight: viewHeight }}
43     >
44       <div className="list-phantom" style={{ height: containerHeight }}></div>
45       <div className="absolute top-0" style={{ top: startIndex * fixedHeight }}>
46         {targetList.map((item) => {
47           return (
48             <div key={item.id} className={""} style={{ height: fixedHeight }}>
49               <span className="name">{item.name}</span>
50               <p className="">{item.body}</p>
51             </div>
52           );
53         })}
54       </div>
55     </div>
56   );
57 };
58 
59 export default VirtualView;

展示元素个数: visibleNum = enindex - startIndex

组件容器:virtual-list-view

因为每行item高度固定, 可以计算的到容器的高度fixedHeight * visibleNum

并注入onscroll回调,let nextIndex = Math.floor(scrollTop / fixedHeight);  根据scrollTop 距离计算当前应该展示的startIndex 和 endIndex

 

支撑容器高度的元素: list-phantom 根据 list.length * fixedHeight 一开始就能得到整个容器的高度     列表元素, 根据startIndex 和 endIndex 对数组数据做切分, 获得应该展示的元素数据, 其实在最后start, end 前后可以再多展示几行数据做缓冲, 避免渲染不连续,但是要记得调整列表元素的top 或者其他位移属性, 嫌麻烦, 这里就不做了   const targetList = list.slice(startIndex, endIndex);

 需要调整top 或其他属性移动到 scrollTop 的距离, 也就是startIndex * fixedHeight

 

 

 

不定高度的版本

 adding...

标签:nextIndex,const,list,列表,React,startIndex,visibleNum,fixedHeight,滚动
From: https://www.cnblogs.com/xlno/p/17718235.html

相关文章

  • 15-Vue核心-列表过滤和列表排序
    列表过滤 监视属性,实现列表过滤<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>基本列表</title><!--引入Vue--><scripttype="text/javascript......
  • 卡片列表布局
    实现代码1<template>2<div>3<!--平铺图片-->4<uiclass="course-list">5<!--设置图片宽度比例-->6<!--循环绑定数据-->7<liclass="course-item"......
  • IIS报错:HTTP 错误 404.2 - Not Found 由于Web服务器上的”ISAPI和CGI限制”列表设置,无
     解决方法:  ......
  • (建议收藏)OpenHarmony系统能力SystemCapability列表
    SysCap,全称SystemCapability,即系统能力,指操作系统中每一个相对独立的特性。开发者使用某个接口进行开发前,建议先阅读Syscap使用指南,了解Syscap的定义和使用指导。再结合下文中的表格判断具体能力集是否支持某个设备,提高开发效率。说明:Default代表了一个功能比较全面的OpenHarmony设......
  • JS 将列表转成树形结构数据
    一、背景联调发现有些后端返回List但是前后需要转树形结构二、实现点击查看代码import_,{isArray}from'lodash'/***将列表转成树形结构数据*@paramnodes列表数据*@paramoptions可选参数,{idKey,pIdKey,childrenKey}*@returns{[]}树形结构数据......
  • 软件测试|Python如何将列表从大到小排序
    简介在编程中,对列表进行排序是一个常见的操作,有时候我们需要将列表按照从大到小的顺序进行排列。Python提供了多种方法来实现这一目标。在本文中,我们将深入探讨几种将列表从大到小排序的方法,帮助您根据不同情况选择最合适的方式。使用sorted()函数Python的sorted()函数可以接收一......
  • 城市区域列表数据 三级联动
    import{regionData,CodeToText}from'element-china-area-data'//引入element组件库中的插件<el-cascaderv-model="areaData":options="options"@change="handleChange"></el-cascader>options:reg......
  • R语言风险价值:ARIMA,GARCH模型,Delta-normal法滚动估计,预测VaR(Value at Risk)和回测分析
    原文链接:http://tecdat.cn/?p=24492原文出处:拓端数据部落公众号介绍此分析的目的是帮助客户构建一个过程,以在给定时变波动性的情况下正确估计风险价值。风险价值被广泛用于衡量金融机构的市场风险。我们的时间序列数据包括1258天的股票收益。为了解释每日收益率方差的一小部......
  • 14-Vue核心-列表渲染
    使用v-for做列表渲染我们可以用 v-for 指令基于一个数组来渲染一个列表,用于展示列表数据。语法:v-for="(item,index)initems" :key="xxx"或者 v-for="(item,index)ofitems" :key="xxx" 这里可以使用 of 替代 in 作为分隔符,因为它更接近JavaScript迭......
  • case02数据结构之列表
    数据结构之列表:全球股票指数【任务1】在Python中使用列表数据结构并向两个列表中分别输入表1-3中的指数名称和收盘价。同时,在包含指数名称的列表中,依次访问"富时100指数""沪深300指数"这两个元素;在包含收盘价的列表中,依次找出"15437.5100""28458.4400"这两个元素的索引值。......