JSFiddle - React, Tailwind, and code Playground

HTML

<body></body>

JavaScript

//this block to be only this example, normally there are not.
  for (var i = 0; i < 100; i++) {
			  var p = document.createElement("p");
			  p.innerText = "Line " + i;
			  document.body.appendChild(p);
  }
  
  var elements = document.querySelectorAll("p");
  var obj = null;
  window.onscroll = function() {
    if (obj != null) {
		  obj.style.backgroundColor = "";
		}
    var top = Number.POSITIVE_INFINITY;
    Array.from(elements).forEach(function(o) {
      var t = o.getBoundingClientRect(o).top;
      if (t > 0.0 && top > t) {
        obj = o;
        top = t;
      }
    });
    if (obj != null) {
				obj.style.backgroundColor = "red";
    }
  };