Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.1 KB

check-if-a-given-dom-rect-is-contained-within-another-dom-rect.mdx

File metadata and controls

40 lines (32 loc) · 1.1 KB
category created openGraphCover title
DOM
2023-12-11
/og/1-loc/check-given-dom-rect-contained-within-another-dom-rect.png
Check if a given DOMRect is contained within another DOMRect

JavaScript version

const isContained = (target, container) => (
    target.left >= container.left &&
    target.left + target.width <= container.left + container.width &&
    target.top >= container.top &&
    target.top + target.height <= container.top + container.height
);

TypeScript version

const isContained = (target: DOMRect, container: DOMRect): boolean => (
    target.left >= container.left &&
    target.left + target.width <= container.left + container.width &&
    target.top >= container.top &&
    target.top + target.height <= container.top + container.height
);

Example

const target = new DOMRect(10, 10, 20, 20);
const container = new DOMRect(0, 0, 40, 40);
isContained(target, container);     // true

See also