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