0
<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?

In the code above, is there any easy way to tell whether the span is shown in the window or not?

Safwan Samsudeen
  • 1,116
  • 4
  • 17
Qian Chen
  • 12,930
  • 14
  • 54
  • 81

1 Answers1

0

The modern way to do this is to use an intersection observer.

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

let observer = new IntersectionObserver(callback, options);
David Bradshaw
  • 10,116
  • 3
  • 33
  • 61
  • This is not related to my question, but it's very interesting. It looks like it will notify when some value changes? – Qian Chen Aug 27 '20 at 08:39