vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
40 lines (39 loc) • 1.23 kB
TypeScript
import type { GeneralWindow } from './shared/types';
interface Position {
top: number;
left: number;
right: number;
bottom: number;
}
interface PositionData extends Position {
/** Position relative to the offset parent */
parent: Position;
/** Position relative to the viewport area */
viewport: Position;
}
/**
* Get the current position of a DOM element, either relative to the offsetParent
* or relative to the document. If the element is the viewport or the window, the
* position of the window is returned.
*
* @param elm - The DOM element to find the position of
* @param relative = false - Find the position relative to the offsetParent rather than the document
* @return the position information of the element
*
* @example
*
* ```ts
* // Get the position of the current window
* position();
*
* // Get the position of the window of a given document
* position(document);
* position(document.documentElement);
* position(document.body);
*
* // Get the position of a given element
* position(someElement);
* ```
*/
export default function position(elm?: HTMLElement | GeneralWindow | Document): Position | PositionData;
export {};