# PositionObserver [](https://coveralls.io/github/thednp/position-observer) [](https://github.com/thednp/position-observer/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@thednp/position-observer) [](https://www.typescriptlang.org/) [](https://vitest.dev/) [](https://vitejs.dev/) If you were looking for an observer that could replace all your `resize` and/or `scroll` EventListeners, this should be it! The **PositionObserver** works with the [IntersectionObserver API](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver) under the hood and uses a very simple design. The **PositionObserver** provides a way to asynchronously observe changes in the position of a target element with an ancestor element or with a top-level document's viewport. It tries to do what you would expect after your element has intersected as if you would listen to `resize` or `scroll` without attaching event listeners. ## Installation ```bash npm i @thednp/position-observer ``` ```bash yarn add @thednp/position-observer ``` ```bash pnpm add @thednp/position-observer ``` ```bash deno add npm:@thednp/position-observer@latest ``` ## Usage ```ts // import the PositionObserver class import PositionObserver, { type PositionObserverEntry } from '@thednp/position-observer'; // find a suitable target const myTarget = document.getElementById('myElement'); // define a callback const callback = (entries: PositionObserverEntry[], currentObserver: PositionObserver) => { /* keep an eye on your entries */ // console.log(entries); // access the observer inside your callback // to find entry for myTarget const entry = currentObserver.getEntry(myTarget); if (entry.target === myTarget/* and/or other conditions */) { // do something about it } }; // set some options const options = { // if not set, it will use the document.documentElement root: document.getElementById('myModal'), } // create the observer const observer = new PositionObserver(callback, options); // start observing the target element position observer.observe(target); // when the position of the element changes from DOM manipulations and/or // the position change was triggered by either scroll / resize events // these will be the entries of this observer callback example [{ // the observed target element target: