本节,咱们实现一个极简版的虚拟列表,固定尺寸的虚拟列表,麻雀虽小,倒是五脏俱全哦!html
实现一个固定尺寸的虚拟渲染列表组件,props属性以下:react
props: { width: number; height: number; itemCount: number; itemSize: number; }
使用方式:app
const Row = (..args) => (<div className="Row"></div>); <List className={"List"} width={300} height={300} itemCount={10000} itemSize={40}> {Row} </List>
什么技术栈均可以,这里项目使用的react,那就选用react来实现。dom
使用create-react-app初始化一个应用,而后启动,清理掉demo的代码。函数
根据上一节的分析,咱们核心技术实现是 一个render渲染函数,用来渲染数据;一个onScroll函数监听滚动事件,去更新 数据区间[startIndex, endIndex],而后从新render。大概伪代码以下:this
class List extends React.PureComponent { state = {}; render() {}; onScroll() {}; }
接下来咱们进行细节填充实现,首先咱们须要根据数据渲染出第一屏初始化的dom,即要先实现render函数逻辑,咱们采用绝对定位的方式进行dom排版。code
render() { // 从props解析属性 const { children, width, height, itemCount, layout, itemKey = defaultItemKey, } = this.props; // 预留方向设定属性 const isHorizontal = layout === "horizontal"; // 假设有一个函数_getRangeToRender能够帮咱们计算出 渲染区间 const [startIndex, stopIndex] = this._getRangeToRender(); const items = []; if (itemCount > 0) { // 循环建立元素 for (let index = startIndex; index <= stopIndex; index++) { items.push( createElement(children, { data: {}, key: itemKey(index), index, style: this._getItemStyle(index), // 帮助计算dom的位置样式 }) ); } } // 假设getEstimatedTotalSize函数能够帮助咱们计算出总尺寸 const estimatedTotalSize = getEstimatedTotalSize( this.props, ); return createElement( "div", { onScroll: this.onScroll, style: { position: "relative", height, width, overflow: "auto", WebkitOverflowScrolling: "touch", willChange: "transform", }, }, createElement("div", { children: items, style: { height: isHorizontal ? "100%" : estimatedTotalSize, pointerEvents: "none", width: isHorizontal ? estimatedTotalSize : "100%", }, }) ); }
OK,到了这里render函数的逻辑就写完了,是否是超级简单。接下来咱们实现如下 render函数里面使用到的辅助函数.orm
先看getEstimatedTotalSize计算总尺寸函数的实现:htm
// 总尺寸 = 总个数 * 每一个size export const getEstimatedTotalSize = ({ itemCount, itemSize }) => itemSize * itemCount;
计算须要渲染的数据区间函数实现索引
_getRangeToRender() { // overscanCount是缓冲区的数量,默认设置1 const { itemCount, overscanCount = 1 } = this.props; // 已经滚动的距离,初始默认0 const { scrollOffset } = this.state; if (itemCount === 0) { return [0, 0, 0, 0]; } // 辅助函数,根据 滚动距离计算出 区间开始的索引 const startIndex = getStartIndexForOffset( this.props, scrollOffset, ); // 辅助函数,根据 区间开始的索引计算出 区间结束的索引 const stopIndex = getStopIndexForStartIndex( this.props, startIndex, scrollOffset, ); return [ Math.max(0, startIndex - overscanCount), Math.max(0, Math.min(itemCount - 1, stopIndex + overscanCount)), startIndex, stopIndex, ]; } } // 计算区间开始索引,滚动距离 除以 每一个单元尺寸 就是 startIndex export const getStartIndexForOffset = ({ itemCount, itemSize }, offset) => Math.max(0, Math.min(itemCount - 1, Math.floor(offset / itemSize))); // 计算区间结束索引,开始索引 + 可见区域size / itemSize 便可 export const getStopIndexForStartIndex = ( { height, itemCount, itemSize, layout, width }, startIndex, scrollOffset ) => { const isHorizontal = layout === "horizontal"; const offset = startIndex * itemSize; const size = isHorizontal ? width : height; const numVisibleItems = Math.ceil((size + scrollOffset - offset) / itemSize); return Math.max( 0, Math.min( itemCount - 1, startIndex + numVisibleItems - 1 ) ); };
计算方式:根据index * itemSize 便可计算出position
_getItemStyle = (index) => { const { layout } = this.props; let style; const offset = index * itemSize; const size = itemSize; const isHorizontal = layout === "horizontal"; const offsetHorizontal = isHorizontal ? offset : 0; style = { position: "absolute", left: offsetHorizontal, top: !isHorizontal ? offset : 0, height: !isHorizontal ? size : "100%", width: isHorizontal ? size : "100%", }; return style; };
好了,到此位置,render函数的全部逻辑所有实现完毕了。
最后一步,只须要监听onScroll事件,更新 数据索引区间,咱们的功能就完善了
// 很是简单,只是一个 setState操做,更新滚动距离便可 _onScrollVertical = (event) => { const { clientHeight, scrollHeight, scrollTop } = event.currentTarget; this.setState((prevState) => { if (prevState.scrollOffset === scrollTop) { return null; } const scrollOffset = Math.max( 0, Math.min(scrollTop, scrollHeight - clientHeight) ); return { scrollOffset, }; }); };
class List extends PureComponent { _outerRef; static defaultProps = { layout: "vertical", overscanCount: 2, }; state = { instance: this, scrollDirection: "forward", scrollOffset: 0, }; render() { const { children, width, height, itemCount, layout, itemKey = defaultItemKey, } = this.props; const isHorizontal = layout === "horizontal"; // 监听滚动函数 const onScroll = isHorizontal ? this._onScrollHorizontal : this._onScrollVertical; const [startIndex, stopIndex] = this._getRangeToRender(); const items = []; if (itemCount > 0) { for (let index = startIndex; index <= stopIndex; index++) { items.push( createElement(children, { data: {}, key: itemKey(index), index, style: this._getItemStyle(index), }) ); } } const estimatedTotalSize = getEstimatedTotalSize( this.props ); return createElement( "div", { onScroll, style: { position: "relative", height, width, overflow: "auto", WebkitOverflowScrolling: "touch", willChange: "transform", }, }, createElement("div", { children: items, style: { height: isHorizontal ? "100%" : estimatedTotalSize, pointerEvents: "none", width: isHorizontal ? estimatedTotalSize : "100%", }, }) ); } _onScrollHorizontal = (event) => {}; _onScrollVertical = (event) => { const { clientHeight, scrollHeight, scrollTop } = event.currentTarget; this.setState((prevState) => { if (prevState.scrollOffset === scrollTop) { return null; } const scrollOffset = Math.max( 0, Math.min(scrollTop, scrollHeight - clientHeight) ); return { scrollOffset, }; }); }; _getItemStyle = (index) => { const { layout } = this.props; let style; const offset = getItemOffset(this.props, index, this._instanceProps); const size = getItemSize(this.props, index, this._instanceProps); const isHorizontal = layout === "horizontal"; const offsetHorizontal = isHorizontal ? offset : 0; style = { position: "absolute", left: offsetHorizontal, top: !isHorizontal ? offset : 0, height: !isHorizontal ? size : "100%", width: isHorizontal ? size : "100%", }; return style; }; // 计算出须要渲染的数据索引区间 _getRangeToRender() { const { itemCount, overscanCount = 1 } = this.props; const { scrollOffset } = this.state; if (itemCount === 0) { return [0, 0, 0, 0]; } const startIndex = getStartIndexForOffset( this.props, scrollOffset ); const stopIndex = getStopIndexForStartIndex( this.props, startIndex, scrollOffset ); return [ Math.max(0, startIndex - overscanCount), Math.max(0, Math.min(itemCount - 1, stopIndex + overscanCount)), startIndex, stopIndex, ]; } }