JSFiddle - React, Tailwind, and code Playground

by S S

HTML

<div class="wrap">
  <div class="foo" id="someDiv">this is a test</div>
</div>

<!--
Turn on your console
-->

CSS

.wrap {
  height: 1500px;
}

.foo {
  height:50px;
  background-color:#000;
  color:#fff;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: auto;
  text-align: center;
  line-height: 50px;
}

JavaScript

window.addEventListener('scroll', function() {

   var someDiv = document.getElementById('someDiv');
   var distanceToTop = someDiv.getBoundingClientRect().top;

   console.log(distanceToTop);
});