Build a Sticky Menu
How to make a sticky menu and change it's visual properties on scroll
Last updated
Was this helpful?
Was this helpful?
<script>
const menu = document.getElementById('fancymenu');
window.addEventListener('scroll', () => {
if (window.scrollY > 800) {
menu.style.setProperty("width", "100%");
menu.style.setProperty("height", "80px");
menu.style.setProperty("margin-top", "0");
menu.style.setProperty("background", "#ffffff");
} else {
menu.style.removeProperty("width");
menu.style.removeProperty("height");
menu.style.removeProperty("margin-top");
menu.style.removeProperty("background");
}
});
</script>
<script>
const menu = document.getElementById('fancymenu');
window.addEventListener('scroll', () => {
if (window.scrollY > 500) {
menu.classList.add('w-auto');
menu.classList.remove('w-[1000px]', 'mt-10');
} else {
menu.classList.add('w-[1000px]', 'mt-10');
menu.classList.remove('w-auto');
}
});
</script>