The PositionObserver interface provides an asynchronous way to observe changes in the position, size, and intersection state of elements relative to their containing root element.
PositionObserver asynchronously monitors changes to element positioning, sizing, and visibility within a specified root container or the viewport. It extends the functionality of the native IntersectionObserver API, and provides detailed position and size information whenever these properties change.
The underlying implementation avoids polling mechanisms and optimizes performance by minimizing calls to getBoundingClientRect(), relying instead on a combination of native browser observation APIs.
- Observes element position changes within the viewport or custom root containers
- Detects element size modifications, including changes due to CSS transformations
- Monitors intersection state between observed elements and their root container
- Detects changes to the position of an element when the viewport or root resizes
-
callback - A function called when position changes are detected
(entries: PositionObserverEntry[]) => void -
options (optional) - Configuration object
{ root?: Element | Document | null; }
A new PositionObserver instance.
Begins observing position changes for the specified element.
- target - The Element to observe for position changes.
Stops observing position changes for the specified element.
- target - The Element to stop observing.
Stops observing all target elements and releases all references.
Represents a single observation result containing geometric information about an observed element.
- target (Element) - The observed element
- boundingClientRect (DOMRectReadOnly) - The element's current position and dimensions
- intersectionRect (DOMRectReadOnly) - The visible portion of the element within the root
- isIntersecting (boolean) - Whether the element intersects with the root
- rootBounds (DOMRectReadOnly) - The root container's dimensions
PositionObserver only triggers callbacks for elements that intersect with the root container. Position changes that occur while an element is completely outside the root bounds will not be detected until the element re-enters the observable area.
PositionObserver may not detect very small position changes (approximately 1 pixel or less) due to the threshold-based nature of the underlying IntersectionObserver API. The detection sensitivity is influenced by the relative size of the observed element and its root container.
Clone the repository and install dependencies:
The test suite uses Playwright for cross-browser testing and includes comprehensive coverage of PositionObserver functionality.
MIT License, refer to LICENSE.md
.png)

