JSFiddle - React, Tailwind, and code Playground
HTML
<div id="menu">
MENU
</div>
CSS
body {
height: 2400px;
}
#menu {
top: 0;
position: sticky;
}
JavaScript
function checkScroll() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const menu = document.getElementById('menu');
menu.style.backgroundColor = scrollTop >= 1200 ? 'green' : 'red';
}
checkScroll();
document.addEventListener('scroll', checkScroll, false);