dom-find
v1.0.0
Published
Find a DOM element's document- and viewport-relative position
Maintainers
Readme
dom-find
Utils for finding a DOM element's position. Used by react-stickydiv.
import { findPos, findPosRelativeToViewport, getPageScroll } from "dom-find";
findPos(document.getElementById("element"));
// [0, 100]
findPosRelativeToViewport(document.getElementById("element"));
// [50, -200]
getPageScroll();
// [0, 40]CommonJS still works:
const { findPos } = require("dom-find");Pass a live Element. null / undefined / non-elements throw a TypeError (for example document.getElementById when the node is missing).
Positions come from Element#getBoundingClientRect plus window.scrollX / window.scrollY, so CSS transforms are included. This is a breaking change from 0.3.x, which walked offsetParent and had Internet Explorer fallbacks.
API
| Function | Returns |
| --- | --- |
| findPos(element) | [x, y] relative to the document |
| findPosRelativeToViewport(element) | [x, y] relative to the viewport |
| getPageScroll() | [x, y] page scroll. [0, 0] when window is missing (SSR) |
