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
41 lines (40 loc) • 1.22 kB
TypeScript
interface PositionIndicator {
/** Inside the viewport area? */
inside: boolean;
/** Above the viewport area? */
above: boolean;
/** Below the viewport area? */
below: boolean;
/** To the left of the viewport area? */
left: boolean;
/** To the right of the viewport area? */
right: boolean;
}
/**
* Determines whether the element is in the area of the viewport or not.
*
* @param elm - DOM element to test
* @param threshold - The distance to the edge of the viewport before
* the element is no longer inside in the viewport area,
* in pixels
*
* @return An object with indications of where the element is compared to the viewport area
*
* @example
*
* ```ts
* // Element inside viewport
* const { inside } = inView(myElement);
* // -> inside === true
*
* // Element outside viewport
* const { inside, below } = inView(myElement);
* // -> inside === false; below === true
*
* // With a threshold
* const { inside } = inView(myElement, 30);
* // -> inside === true
* ```
*/
export default function inView(elm: HTMLElement, threshold?: number): PositionIndicator;
export {};