1

Someone knows how to reference the "nearest to the top" dom element currently on visible screen?

Something like this solution: How to get first visible DOM element currently showing on the screen? ; but purely on javascript.

Don't care about position coords, nor anything else, than just get reference to it.

Thanks in advance

NBK
  • 58
  • 6

2 Answers2

2

This is basically the same code as this answer but without jQuery.

function visible(elem) {
  return !(elem.clientHeight === 0 || elem.clientWidth === 0)
}

let first;
let firstOffY;
let allVisible = Array.from(document.querySelectorAll('body > *')).filter(visible)
for(const elem of allVisible) {
  //Calculaate the offset to the document 
  //See: https://stackoverflow.com/a/18673641/7448536
  const offY = elem.getBoundingClientRect().top + document.documentElement.scrollTop
  if (first == null || offY < firstOffY) {
    first = elem;
    firstOffY = offY;
  }
}

first.classList.add('highlight');
.highlight {
  background-color: yellow;
}
<div>
  <p>Some Text</p>
  <h1>Headline</h1>
</div>
  • document.querySelectorAll('body > *') Select all elements under body
  • Array.from Convert the return value of querySelectorAll to a real array
  • .filter(visible) discard all elements that are not visible
Wendelin
  • 2,354
  • 1
  • 11
  • 28
1

In case you need better compatibility, here a bit adjusted Wendelin's answer.

function visible(elem) {
  return !(elem.clientHeight === 0 || elem.clientWidth === 0)
}

let first;
let firstOffY;
var elems = [], source = document.querySelectorAll('body > *');
for(var elemI=0;elemI<source.length;elemI++) {
   elems.push(source[elemI]);
}
let allVisible = elems.filter(visible)
for(var elem in allVisible) {
  elem = allVisible[elem];
  //Calculaate the offset to the document 
  //See: https://stackoverflow.com/a/18673641/7448536
  const offY = elem.getBoundingClientRect().top + document.documentElement.scrollTop
  if (first == null || offY < firstOffY) {
    first = elem;
    firstOffY = offY;
  }
} 

console.log(first.outerHTML);
first.classList.add('highlight');
.highlight {
  background-color: yellow;
}
<div>
  <p>Some Text</p>
  <h1>Headline</h1>
</div>
Jan
  • 2,178
  • 3
  • 14
  • 26