Created
March 5, 2020 13:33
-
-
Save sueLan/d1c37bc5728709b3d8d9561b7e2914e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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