[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/API/Intersection_Observer_API [Back]  [Original]

Intersection Observer API - API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Intersection Observer API

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2019 ..

* Some parts of this feature may have varying levels of support.

Intersection Observer API - viewport.

. . , "", , :

  • .
  • - " ", , .
  • .
  • , - , .

, Element.getBoundingClientRect(), . , .

- . , , . , . , , . , , , , .

Intersection Observer API -, ( viewport), . , , .

Observer API ; , " N%, -".

In this article

Intersection Observer API , (target) ( ) (root).

, ( null ).

, API , -, , , .

0.0 1.0, 1.0 .

-, :

js
var options = {
  root: document.querySelector("#scrollArea"),
  rootMargin: "0px",
  threshold: 1.0,
};
var callback = function (entries, observer) {
  /* Content excerpted, show below */
};
var observer = new IntersectionObserver(callback, options);

threshold 1.0 100% ( ) root

root

. . null.

rootMargin

root. css margin: "10px 20px 30px 40px" (top, right, bottom, left). . .

threshold

, , callback. , callback 25% : [0, 0.25, 0.5, 0.75, 1]

,

, , :

js
var target = document.querySelector("#listItem");
observer.observe(target);

, , IntersectionObserver, - callback. callback IntersectionObserverEntry :

js
var callback = function (entries, observer) {
  entries.forEach((entry) => {
    entry.time; // a DOMHightResTimeStamp indicating when the intersection occurred.
    entry.rootBounds; // a DOMRectReadOnly for the intersection observer's root.
    entry.boundingClientRect; // a DOMRectReadOnly for the intersection observer's target.
    entry.intersectionRect; // a DOMRectReadOnly for the visible portion of the intersection observer's target.
    entry.intersectionRatio; // the number for the ratio of the intersectionRect to the boundingClientRect.
    entry.target; // the Element whose intersection with the intersection root changed.
    entry.isIntersecting; // intersecting: true or false
  });
};

, - , - , Window.requestIdleCallback().

, root, .

IntersectionObserver

API Intersection Observer. observer, . observer - Document's viewport. root.

IntersectionObserverEntry

. : IntersectionObserver IntersectionObserver.takeRecords().

Specification
Intersection Observer
# intersection-observer-interface


Web Proxy Viewer  |  New URL  |  Original Page