export default function isElementVerticallyInView ( container , element , partial ? ) { if ( container && element ) { const cTop = container . scrollTop ; const cBottom = cTop + container . clientHeight ; const eTop = element . offsetTop ; const eBottom = eTop + element . clientHeight ; const isTotal = eTop >= cTop && eBottom <= cBottom ; const isPartial = partial && ( ( eTop < cTop && eBottom > cTop ) || ( eBottom > cBottom && eTop < cBottom ) ) ; return isTotal || isPartial ; } } export function isElementHorizontallyInView ( container , element , partial ? ) { if ( container && element ) { const cLeft = container . scrollLeft ; const cRight = cLeft + container . clientWidth ; const eLeft = element . offsetLeft ; const eRight = eLeft + element . clientWidth ; const isTotal = eLeft >= cLeft && eRight <= cRight ; const isPartial = partial && ( ( eLeft < cLeft && eRight > cLeft ) || ( eRight > cRight && eLeft < cRight ) ) ; return isTotal || isPartial ; } return false ; } export function verticallyScrollElementIntoView ( container , element , smooth ? ) { if ( container && element ) { const cTop = container . scrollTop ; const cBottom = cTop + container . clientHeight ; const eTop = element . offsetTop ; const eBottom = eTop + element . clientHeight ; if ( smooth ) { if ( eTop < cTop || eBottom > cBottom ) { element . scrollIntoView ( { behavior : 'smooth' } ) ; } } else { if ( eTop < cTop ) { container . scrollTop -= cTop - eTop ; } else if ( eBottom > cBottom ) { container . scrollTop += eBottom - cBottom ; } } } } export function horizontallyScrollElementIntoView ( container , element , smooth ? ) { if ( container && element ) { const cLeft = container . scrollLeft ; const cRight = cLeft + container . clientWidth ; const eLeft = element . offsetLeft ; const eRight = eLeft + element . clientWidth ; let targetScrollLeft ; if ( eLeft < cLeft || ( eLeft < cLeft && eRight > cLeft ) ) { targetScrollLeft = container . scrollLeft - cLeft - eLeft ; } else if ( eRight > cRight || ( eRight > cRight && eLeft < cRight ) ) { targetScrollLeft = container . scrollLeft + eRight - cRight ; } if ( smooth ) { const opts = { top : 0 , left : targetScrollLeft , behavior : 'smooth' , } ; container . scrollTo ( opts ) ; } else { container . scrollLeft = targetScrollLeft ; } } }