Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to check if element is in viewport

function isInViewport(element) {
    const rect = element.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= ((window.innerHeight + rect.height) || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

//optimized from the stackOverflow answer to account 
//for element heights and widths (in vertical/horizontal scrolling)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #check #element #viewport
ADD COMMENT
Topic
Name
5+7 =