/** * 判断元素是否在滚动区域内 * element: 目标元素dom * scrollContainer: 滚动区域 */ export function isElementInScrollArea(element, scrollContainer) { const elementRect = element.getBoundingClientRect() const containerRect = scrollContainer.getBoundingClientRect() // 判断元素的上边界和下边界是否在滚动容器的上边界和下边界之间 const isElementAboveContainer = elementRect.bottom < containerRect.top const isElementBelowContainer = elementRect.top > containerRect.bottom // 如果元素在滚动容器的上下边界之间,则认为它在滚动区域内 return !(isElementAboveContainer || isElementBelowContainer) }