I am trying to create a frontend logic here, and the requirement is as follows -:
- There are 4 UI elements on the screen.
- On certian device resolutions, some of these UI elements overflow outside of the screen or get hidden behind the main header component.
- I want to prompt the user to change the zoom level of the browser till all of the 4 UI elements are completely visible in their respective position or not.
Initially, I thought of using the Intersection Observer to check the Intersection Ratio of the 4 UI elements. But, I am confused if the intersections observer works when elements are hidden visually, but present on the screen DOM wise?
You are basically asking two questions:
That's exactly what intersection observer is for.
Sadly, there is no top-level native API similar to intersection observer for this. You can only query "what is the topmost element on given coordinates" with
elementFromPoint
.So technically it is possible to detect if some element is (partially) obscured by other element, and what obscures it, with combinations of
getBoundingClientRect
(orgetClientRects
) andelementFromPoint
. See https://stackoverflow.com/a/1542908/540955 for a simple POC.