How can I determine the distance of the element from the bottom of the visual area?

I want to press the up and down keys to achieve the selection function, but if the selected element reaches the bottom of the visual area, pressing the key again requires the page to scroll down, but how to judge the distance between the top or bottom of the visible area in the element?
Thank you!

May.05,2022

if the height of each row is fixed, change the scrollTop of the scroll bar.

idea 1:
scrollTop = index * height of each row (this makes the selected data always in the first place)

idea 2: to determine whether the element is invisible or not, we need to adjust the height of the scroll bar
downward invisible: (index+1) * height-popHeight (drop-down box height) > scrollTop
upward invisible: index * height < scrollTop


function checkShow(ele) { // jq
    var scrollTop = $(window).scrollTop();  //
    var windowHeight = $(window).height(); // 
    var offsetTop = ele.offset().top;  //document

    if (offsetTop < (scrollTop + windowHeight) && offsetTop > scrollTop) { //2
        return true;
    }
    return false;
}
Menu