checkVisibility Guards Offscreen Rendering

checkVisibility reports whether an element actually renders; display:none and detachment fail while visibility and opacity need explicit check options.

JavascriptData Fetching

Code

javascriptsnippet
21 lines
// checkVisibility 检查元素是否真的会被渲染:display:none、脱离文档都算不可见
const probe = document.createElement('div');
probe.textContent = 'probe';
document.body.append(probe);

console.log(probe.checkVisibility()); // 在文档里 → true

// display:none 不参与渲染 → false
probe.style.display = 'none';
console.log(probe.checkVisibility());

// visibility:hidden 默认不算数,要传 checkVisibilityCSS 才检查
probe.style.display = 'block';
probe.style.visibility = 'hidden';
console.log(probe.checkVisibility());
console.log(probe.checkVisibility({ checkVisibilityCSS: true }));

// opacity:0 同理,要传 checkOpacity
probe.style.visibility = 'visible';
probe.style.opacity = '0';
console.log(probe.checkVisibility({ checkOpacity: true }));

checkVisibility Guards Offscreen Rendering, line by line

Demo state sits in top-level bindings: probe.

What to watch for

  • Zero-fetch snippet: every identifier resolves inside the language distribution.
  • document exist only in browsers — a Node runtime has no shims for them out of the box.

Key statements

const probe = document.createElement('div');
probe.style.visibility = 'visible';
probe.style.visibility = 'hidden';

Interactive Tools for This Topic

FAQ

Does checkVisibility Guards Offscreen Rendering 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.