JSFiddle - React, Tailwind, and code Playground
HTML
<div>Блок</div>
CSS
body {
height: 3000px;
}
div {
width: 100%;
height: 100px;
background-color: #333;
color: #fff;
text-align: center;
}
JavaScript
let block = document.querySelector('div');
window.addEventListener('scroll', function() {
if (block.offsetHeight <= window.pageYOffset) {
block.style.top = '0'
block.style.position = 'fixed'
} else {
block.style.removeProperty('top')
block.style.removeProperty('position')
}
});