real-visibility.js - check real visibility of DOM element (el).
Why I may need this? If you create web ui with multiple widgets, you often need a way to detect that some widget is out of user visible area, so you can safely stop activities within the widget. Visibility api covers only part of cases, it works on page level, not on widget level.
Other public sulutions, which I saw, are not fully satisfy this goal.
-
Check is document page visible or hidden via page visibility api, if possible.
-
Get bounding of element by rectPos = el.getBoundingClientRect().
-
Get elements (by document.elementFromPoint), placed on positions (rectPos.left, rectPos.top), (rectPos.left, rectPos.bottom - 1), (rectPos.right - 1, rectPos.top), (rectPos.right - 1, rectPos.bottom - 1), and compare elements with the el.
-
If at least one these elements is same as el, or contains el, or el contains it, then the el is visible.
-
Include real-visibility.js on your html page (or load dynamically via some javascript loader ). Functions will be added to DOM prototype (will be global for every DOM element): isReallyNear() and isReallyVisible().
-
Call on some DOM element (not on jquery selector!) function isReallyVisible().
let visible = document.querySelectorAll('body div')[0].isReallyVisible();