【问题标题】:Is there any easy way to tell if an element is shown in CSS? [duplicate]有什么简单的方法可以判断一个元素是否显示在 CSS 中? [复制]
【发布时间】:2020-08-27 16:29:02
【问题描述】:
<div style="display: none;">
   <span>Hello</span>
</div>
const div = document.querySelector('div');
// getComputedStyle(div).display === 'none';

const span = document.querySelector('span');
// how can I tell if the span is shown?

在上面的代码中,有没有简单的方法来判断span是否显示在窗口中?

【问题讨论】:

  • 你能进一步澄清你的问题吗? const 在我看来不像 CSS
  • 这是 Javascript 代码。
  • 感谢@johannchopin,看来CSS选择器没有简单的方法?
  • @QianChen 我认为仅使用 css 是不可能的

标签: css


【解决方案1】:

执行此操作的现代方法是使用交叉点观察器。

let options = {
  root: document.querySelector('#scrollArea'),
  rootMargin: '0px',
  threshold: 1.0
}

let observer = new IntersectionObserver(callback, options);

【讨论】:

  • 这与我的问题无关,但很有趣。看起来它会在某些值发生变化时通知?
猜你喜欢
  • 2012-09-03
  • 2010-10-06
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多