<script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
const { useState,useEffect,useRef } = React;
const testRef = useRef(null);
const [result, setResult] = useState('');
const handleScroll = () => {
const itemOffset = testRef.current.offsetTop;
const scrollTop = window.scrollY;
if (scrollTop >= itemOffset) {
setResult('Item has reached the top');
window.addEventListener('scroll', handleScroll);
window.removeEventListener('scroll', handleScroll);
<div className='container' style={{ height: '600px' }}>
React JS Dynamically check if elements hit top of window
{result && <small className="result">{result}</small>}
ReactDOM.render(<App />, document.getElementById('app'));
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transform: translateX(-50%);
transition: opacity 0.3s ease-in-out;