Intersection API

  • This is a list

  • siamo quasi

  • eccolo

Intersection Observer API

The Intersection Observer is a web platform API, included in JS and without running in the main thread.

it asynchronously observe intersections between the target elements and the user viewpoint root:

//viewpoint set in options and target set in observe()
//IntersectionObs config can't be changed once set but can observe multiple
let base = useRef()
let target = useRef()
  
let interObs = useRef();
useEffect(()=>{

  let options={
    root: base.current, //Document/Element bounding-box used as viewpoint
    rootmargin: "0px",  //offset margins applied to viewpoint intersections
    //interception area percentages that trigger the callback.
    threshold: [...Array(100).keys()].map(x => x / 100) 
  }
  
  //The argument being the observe() target elements
  //of which only some are entries[0].isIntersecting
  function entered(entries){
    console.log( entries[0] )
  }

  interObs.current = new IntersectionObserver(entered, options)
  interObs.current.observe(target.current)
}, [])

We need the IntersectionObserver() object and the target to unobserve() :

We observe() loop when there are multiple intersection targets.

We can use rootMargin to create a 0px top intersect for the root.

The entries of the callback function will be the current inView from the root, with true/false isIntersect based on the (options).

The toggle() method force argument only adds/removes on true/false. We can add a scroll() event with the callback function, but it won't return any target data.

chevron-rightIntersectionRatio, boundingClientRect and intersectionRecthashtag

The intersectionRatio entry prop is the target intersection percentage to the root.

The boundingRect prop is the target position relative to the entire root, its top becomes negative once it's scrolled over, with the formula bottom - top = height.

The intercectRect is the rectangle area where the target and root viewpoint overlay, it's 0 when no intersect, its bottom-top returns the current intersect height of the target, and the top prop stops updating once it has been intersected and no longer visible (unlike boundingRect)

We can modify both the root and target without needing to change the intersectObserver().

interceptionObserver() animated sections

This is how we animate intersectionObserver() nav-items.

Instead of the href/id scroll, we can manually scrollInView the section elements using offsetTop.

The absolute scroll area won't return any height to its relative parent container, so we use document.offsetHeight for design's sake.

We cache IntersectionObserver ID querySelect() outside the intersect function for easier manipulation and access.

Intersection CSS style animation

Check this webpage:

React js version

We animate the sections with toggle() css keyframes.

We can access tag attributes or modify the entry.target.style directly.

1

We apply the Intersection Observer API on the <Parallax> root, to limit the intersection to the scroll window of its layers, and not the entire page.

Last updated