@opensig/opendesign
Version:
25 lines (24 loc) • 561 B
JavaScript
function findIndexByOffset(length, scrollOffset, accessor) {
if (length === 0) {
return 0;
}
let start = 0;
let end = length - 1;
while (start < end) {
const mid = Math.floor((start + end) / 2);
const top = accessor.getTop(mid);
const bottom = accessor.getBottom(mid);
if (top <= scrollOffset && bottom > scrollOffset) {
return mid;
}
if (bottom <= scrollOffset) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return Math.max(0, Math.min(start, length - 1));
}
export {
findIndexByOffset
};