Skip to content

Instantly share code, notes, and snippets.

@amcdnl
Last active June 3, 2018 16:17
Show Gist options
  • Save amcdnl/221a114e9c346b52e3a50d0d8b2c1b36 to your computer and use it in GitHub Desktop.
Save amcdnl/221a114e9c346b52e3a50d0d8b2c1b36 to your computer and use it in GitHub Desktop.
const avgSize = 20;
const scrollOffset = 225;
function measureRangeSize({ start, end }) {
const size = 15;
return (start + end) * size;
}
function getOffset(range, index) {
const isInView = index > range.start && index < range.end;
const direction = index < range.start ? 'before' : 'after';
const start = direction === 'before' ? index : range.start;
const end = isInView ? index : range.end;
const delta = direction === 'before' ? index - start : index - end;
let offset = 0;
const renderedSize = measureRangeSize({
start,
end
});
if (!isInView) {
if (direction === 'before') {
offset = renderedSize - scrollOffset;
} else {
offset += offset + (delta * avgSize);
}
}
return offset;
}
// const res = getOffset({ start: 0, end: 15 }, 5);
// console.log('In range:', res === 75);
const res2 = getOffset({ start: 15, end: 20 }, 5);
console.log('In range:', res2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment