Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection Observer API: Observe the center of the viewport

I am trying to observe the intersection at center of viewport, I have tried with passing negative values in rootMargin

rootMargin: '-45% 10% -45%'

But it doesn't seems to be working properly.

My requirement is to observe the intersection when my element is reaching the center of screen.

like image 850
Nutan Gwari Avatar asked Feb 21 '19 12:02

Nutan Gwari


People also ask

What is the intersection Observer API?

The Intersection Observer API lets code register a callback function that is executed whenever an element they wish to monitor enters or exits another element (or the viewport), or when the amount by which the two intersect changes by a requested amount.

Can intersection observer observe multiple elements?

observe() The IntersectionObserver method observe() adds an element to the set of target elements being watched by the IntersectionObserver . One observer has one set of thresholds and one root, but can watch multiple target elements for visibility changes in keeping with those.

What is root in intersection observer?

root. The IntersectionObserver interface's read-only root property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the root is null , then the bounds of the actual document viewport are used.


1 Answers

Intersection Observer rootMargin and how intersections are handled could become a bit confusing after a couple of tries, so I'll try to elaborate how could you make this happen for a specific scenario:

  • Detect the intersection of an element when its top or bottom reaches the vertical center of the viewport

Intersecting element's top/bottom at the center of the viewport

const intersectionAtTopOrBottomElement = document.querySelector('.top-bottom-element');

const elementHasIntersected = (entries, o) => {...};
const ioConfiguration = {
  /**
   * This rootMargin creates a horizontal line vertically centered
   * that will help trigger an intersection at that the very point.
   */
  rootMargin: '-50% 0% -50% 0%',

  /**
   * This is the default so you could remove it.
   * I just wanted to leave it here to make it more explicit
   * as this threshold is the only one that works with the above
   * rootMargin
   */
  threshold: 0
};

const observer = new IntersectionObserver(elementHasIntersected, ioConfiguration);
observer.observe(intersectionAtTopOrBottomElement);

By doing this the elementHasIntersected callback will get called as soon as the element's most top or most bottom edge intersects with the center of the viewport.

Fairly easy right? Now, if you'd like to achieve a similar "center intersection" scenario such as:

  • Detect the intersection of an element when its vertical center reaches the vertical center of the viewport

Then, this would need a different approach as the rootMargin: '-50% 0% -50% 0%' would never allow the intersection to be triggered at 50%–as the element would never be 50% visible. I'd be happy to brainstorm about this specific scenario if it's valuable to you, so let me know.

Here's an article I posted about the Intersection Observer API and The Intersection Observer Playground I put together where you can try different configurations (this version has some limits) and maybe you'll find the combination you need.

like image 182
wilsotobianco Avatar answered Oct 02 '22 20:10

wilsotobianco