示例工程 19-virtualized-lists
19-virtualized-lists/demo-01-window-range/range.ts
type WindowRange = { start: number; end: number; total: number };
function computeRange(
count: number,
scrollTop: number,
viewport: number,
rowHeight: number,
overscan = 4,
): WindowRange {
const total = count * rowHeight;
const rawStart = Math.floor(Math.max(0, scrollTop) / rowHeight);
const start = Math.max(0, rawStart - overscan);
const visible = Math.ceil(viewport / rowHeight);
const end = Math.min(count, start + visible + overscan * 2);
return { start, end, total };
}
const topRange = computeRange(10_000, 0, 320, 36);
const middle = computeRange(10_000, 5000 * 36, 320, 36);
const bottom = computeRange(10_000, 10_000 * 36, 320, 36);
console.log(
JSON.stringify({
evt: "smoke",
ok:
topRange.start === 0 &&
middle.start === 4996 &&
middle.end < 5060 &&
bottom.end === 10_000 &&
middle.total === 360_000,
top: topRange,
middle,
bottom,
}),
);