Skip to content
the poor coder

IntersectionObserver

Poor Man
Published
Reading time
2 min read
On this page

What is IntersectionObserver?#

IntersectionObserver is an API that allows JavaScript to observe changes in the intersection of a target element with an ancestor element or the browser’s viewport. It is useful when you want to know if an element is visible on the screen or not.

How does it work?#

The IntersectionObserver API works by creating an instance of the IntersectionObserver class and passing in a callback function, as well as an options object. The callback function will be called every time the observed element intersects with another element, or when it becomes visible or hidden on the screen.

Text
const observer = new IntersectionObserver((entries, observer) => {
  // Do something when target element intersects with other element or viewport
}, options);

observer.observe(targetElement);

The entries parameter in the callback function is an array of IntersectionObserverEntry objects, which provide information about each observation. The observer parameter is a reference to the observer object itself.

Options#

The options object passed to the IntersectionObserver constructor can contain several properties:

  • root: The element that is used as the viewport for checking visibility. Defaults to the browser viewport if not specified.
  • rootMargin: A string representing the margin around the root element. Can have values similar to CSS margin property, e.g. "10px 20px".
  • threshold: A number or array of numbers indicating at what percentage of the target's visibility the observer's callback should be executed.

Examples#

Here are some examples of using IntersectionObserver:

Text
// Observe when an element enters or leaves the viewport
const observer = new IntersectionObserver((entries, observer) => {
  for (const entry of entries) {
    if (entry.isIntersecting) {
      // Element is now visible on the screen
    } else {
      // Element is no longer visible on the screen
    }
  }
});

observer.observe(targetElement);

// Observe when an element intersects with another element
const observer = new IntersectionObserver((entries, observer) => {
  for (const entry of entries) {
    if (entry.intersectionRatio > 0) {
      // Element is intersecting with other element
    } else {
      // Element is not intersecting with other element
    }
  }
}, { root: document.querySelector('#container') });

observer.observe(targetElement);

You can also unobserve and disconnect an observer:

Text
// Stop observing a target element
observer.unobserve(targetElement);

// Disconnect the observer completely
observer.disconnect();

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC