Created
March 5, 2020 13:29
-
-
Save sueLan/0353a3eb5181da79c2ad5e71ba924e40 to your computer and use it in GitHub Desktop.
This file contains hidden or 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