Intersection Observer API
The IntersectionObserverEntry() constructor creates and returns a new IntersectionObserverEntry object.
[!NOTE] In typical usage, you don't need to call this constructor yourself.
IntersectionObserverEntryobjects are created automatically by the browser and delivered to the IntersectionObserver callback when an intersection is observed, or returned by IntersectionObserver.takeRecords().
Syntax
new IntersectionObserverEntry(intersectionObserverEntryInit)
Parameters
intersectionObserverEntryInit- : An object with the following properties, all of which are required:
boundingClientRect- : An object specifying the location and dimensions of the target element's bounding rectangle, with
x,y,width, andheightproperties. This corresponds to the rectangle returned by Element.getBoundingClientRect().
- : An object specifying the location and dimensions of the target element's bounding rectangle, with
intersectionRatio- : A number representing the ratio of the
intersectionRectarea to theboundingClientRectarea. If theboundingClientRectarea is zero, this is 1 ifisIntersectingistrue, and 0 if not.
- : A number representing the ratio of the
intersectionRect- : An object specifying the location and dimensions of the target's visible area within the root's intersection rectangle, with
x,y,width, andheightproperties.
- : An object specifying the location and dimensions of the target's visible area within the root's intersection rectangle, with
isIntersecting- : A boolean value which is
trueif the target element intersects with the intersection observer's root, orfalseotherwise.
- : A boolean value which is
isVisible- : A boolean value which is
trueif the target element has been determined to be fully visible and unoccluded, with no visual effects that would alter its display on screen. A value offalsemeans either that the target has compromised visibility, or that this determination could not be made.
- : A boolean value which is
rootBounds- : An object specifying the location and dimensions of the root's intersection rectangle, with
x,y,width, andheightproperties, ornull.
- : An object specifying the location and dimensions of the root's intersection rectangle, with
target- : The Element whose intersection with the root changed.
time- : A DOMHighResTimeStamp indicating the time at which the intersection was recorded, relative to the
IntersectionObserver's time origin.
- : A DOMHighResTimeStamp indicating the time at which the intersection was recorded, relative to the
- : An object with the following properties, all of which are required:
Return value
A new IntersectionObserverEntry object whose properties are initialized to the values specified in intersectionObserverEntryInit.
Examples
Creating an IntersectionObserverEntry
This example creates a basic IntersectionObserverEntry describing a fully visible element. While you can construct an entry manually like this, in practice these objects are created by the browser and passed to your IntersectionObserver callback automatically.
const entry = new IntersectionObserverEntry({
time: performance.now(),
rootBounds: { x: 0, y: 0, width: 1024, height: 768 },
boundingClientRect: { x: 10, y: 20, width: 200, height: 100 },
intersectionRect: { x: 10, y: 20, width: 200, height: 100 },
isIntersecting: true,
isVisible: false,
intersectionRatio: 1.0,
target: document.body,
});
Specifications
Browser compatibility
See also
- IntersectionObserverEntry
- IntersectionObserver
- Intersection Observer API
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
