checkVisibility Reports Element Render State
Call checkVisibility once, or with checkOpacity and checkVisibilityCSS options, to ask whether an element is actually rendered instead of guessing.
Code
function visibilityReport(id: string): string {
const el = document.querySelector<HTMLElement>(`#${CSS.escape(id)}`);
if (!el) {
return `#${id}: not found`;
}
// checkVisibility answers "does this element actually render?" in
// one call instead of chaining getComputedStyle and offsetParent
if (!el.checkVisibility()) {
return `#${id}: detached or display:none`;
}
// Option objects widen the check: zero opacity and visibility:hidden
// both count as invisible, matching what a user can see
const fullyVisible = el.checkVisibility({
checkOpacity: true,
checkVisibilityCSS: true,
});
return fullyVisible ? `#${id}: fully visible` : `#${id}: rendered but transparent`;
}
for (const id of ['banner', 'ghost', 'badge']) {
console.log(visibilityReport(id));
}One call instead of a computed-style chain
visibilityReport(id) looks the element up with document.querySelector<HTMLElement>(#${CSS.escape(id)}) and returns #${id}: not found immediately when !el. The first el.checkVisibility() answers “does this element actually render?” in one call — the file’s comment contrasts it with chaining getComputedStyle and offsetParent by hand — and reports #${id}: detached or display:none when it fails.
The option object that widens the check
A second call passes { checkOpacity: true, checkVisibilityCSS: true }, so zero opacity and visibility: hidden both count as invisible. The return value splits on that: #${id}: fully visible or #${id}: rendered but transparent. The loop drives three ids through the same ladder — 'banner', 'ghost', 'badge' — logging one report line each.
The line that decides the label
return fullyVisible ? `#${id}: fully visible` : `#${id}: rendered but transparent`;
Everything it touches — document, CSS.escape, element methods — exists only in the browser, so open a page or the DevTools console to exercise it.
Interactive Tools for This Topic
FAQ
- Does checkVisibility Reports Element Render State run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.