How do I check if an element is visible on the screen in JavaScript?
Davide S
davide s profile pic

To check if an element is visible on the screen in JavaScript, you can use various techniques depending on your requirements. Here are a few common approaches: 1. Using thegetBoundingClientRect() method: - ThegetBoundingClientRect() method returns the size and position of an element relative to the viewport. - You can use the properties of the returnedDOMRect object (top,left,right,bottom) to determine if the element is within the viewport.

1
2
3
4
5
6
7
8
9
10
11

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

In this example, theisElementVisible() function takes anelement as input. It retrieves theDOMRect object usinggetBoundingClientRect() and checks if thetop andleft coordinates are non-negative and if thebottom andright coordinates are within the viewport dimensions. 2. Using theIntersection Observer API: - TheIntersection Observer API provides a way to asynchronously observe changes in the intersection of an element with its parent or the viewport. - You can create anIntersectionObserver instance and use itsobserve() method to start observing an element. TheisIntersecting property of the observed entries indicates whether the element is visible or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

   function isElementVisible(element) {
     const observer = new IntersectionObserver((entries) => {
       entries.forEach((entry) => {
         if (entry.isIntersecting) {
           // Element is visible
         } else {
           // Element is not visible
         }
       });
     });

     observer.observe(element);
   }
   

In this example, theisElementVisible() function takes anelement as input. It creates anIntersectionObserver instance and defines a callback function that handles the visibility changes. Inside the callback, you can perform actions based on theisIntersecting property of the observed entries. 3. Using theoffsetTop andoffsetLeft properties: - TheoffsetTop andoffsetLeft properties provide the offset of an element relative to its offset parent. - You can compare these properties with the scroll position and the dimensions of the viewport to determine if the element is within the visible area.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

   function isElementVisible(element) {
     const top = element.offsetTop;
     const left = element.offsetLeft;
     const bottom = top + element.offsetHeight;
     const right = left + element.offsetWidth;
     const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
     const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
     const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
     const viewportWidth = window.innerWidth || document.documentElement.clientWidth;

     return (
       top >= scrollTop &&
       left >= scrollLeft &&
       bottom <= scrollTop + viewportHeight &&
       right <= scrollLeft + viewportWidth
     );
   }
   

In this example, theisElementVisible() function takes anelement as input. It retrieves thetop,left,bottom, andright coordinates of the element using theoffsetTop,offsetLeft,offsetHeight, andoffsetWidth properties. It also obtains the scroll position and viewport dimensions to determine if the element is within the visible area. Choose the method that suits your needs. The first method usinggetBoundingClientRect() is suitable for checking if an element is fully visible within the viewport. The second method using theIntersection Observer API is more efficient when you need to track visibility changes or check if an element is partially visible. The third method usingoffsetTop andoffsetLeft properties is useful when you want to determine if an element is within the visible area regardless of scroll position.