Skip to content

Instantly share code, notes, and snippets.

@sueLan
Created March 5, 2020 13:33
Show Gist options
  • Save sueLan/d1c37bc5728709b3d8d9561b7e2914e0 to your computer and use it in GitHub Desktop.
Save sueLan/d1c37bc5728709b3d8d9561b7e2914e0 to your computer and use it in GitHub Desktop.
function _isViewable(
viewAreaMode: boolean,
viewablePercentThreshold: number,
top: number,
bottom: number,
viewportHeight: number,
itemLength: number,
): boolean {
if (_isEntirelyVisible(top, bottom, viewportHeight)) {
// Entirely visible
return true;
} else {
// Get viewable height of this item cell
const pixels = _getPixelsVisible(top, bottom, viewportHeight);
// Get the viewable percentage of this item cell
const percent =
100 * (viewAreaMode ? pixels / viewportHeight : pixels / itemLength);
return percent >= viewablePercentThreshold;
}
}
function _getPixelsVisible(
top: number,
bottom: number,
viewportHeight: number,
): number {
const visibleHeight = Math.min(bottom, viewportHeight) - Math.max(top, 0);
return Math.max(0, visibleHeight);
}
function _isEntirelyVisible(
top: number,
bottom: number,
viewportHeight: number,
): boolean {
return top >= 0 && bottom <= viewportHeight && bottom > top;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment