Skip to content

Intersection Observer API and its application #163

@reboottime

Description

@reboottime

Overview

This article talks about the application of Intersection Observer API and its compatibility consideration.

The basic API

const options = {
  root: document.querySelector("#scrollArea"),
  rootMargin: "0px",
  threshold: 1.0,
};

let observer = new IntersectionObserver(callback, options);
  • The threshold: A threshold of 1.0 means that when 100% of the target is visible within the element specified by the root option, the callback is invoked.
  • The callback:
let callback = (entries, observer) => {
  entries.forEach((entry) => {
    // Each entry describes an intersection change for one observed
    // target element:
    //   entry.boundingClientRect
    //   entry.intersectionRatio
    //   entry.intersectionRect
    //   entry.isIntersecting
    //   entry.rootBounds
    //   entry.target
    //   entry.time
  });
};

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions