I've set up a header component that contains both a slim menu (which disappears when scrolling down) and a sticky navigation bar (always visible, becomes fixed to the top and expands in size when the slim menu vanishes).
However, I am facing an issue with an img
element that should always remain visible above the sticky-nav
(currently only shows when the sticky-nav
is fixed). How can I modify my header component so it allows this image to be positioned "on top" of other page elements, serving as a background for the sticky-nav
?
Desired look (at all times): https://i.sstatic.net/Xq53j.png
const wrapper = document.querySelector('.wrapper');
const skinnyBanner = document.querySelector('.skinny-banner');
let previousScrollY = window.scrollY;
function updateSticky() {
const currentScrollY = window.scrollY;
if (currentScrollY > skinnyBanner.offsetHeight) {
wrapper.classList.add('isSticky');
} else {
wrapper.classList.remove('isSticky');
}
previousScrollY = currentScrollY;
}
window.addEventListener('scroll', updateSticky);
body {
margin: 0;
height: 200vh;
}
.skinny-banner {
background: lightblue;
height: 40px;
display: flex;
}
.nav-menu {
display: flex;
}
.sticky-nav {
background: salmon;
padding: 0 16px;
height: 120px;
opacity: 50%;
backdrop-filter: blur(20px);
}
.sticky-nav ul {
display: flex;
}
/* Styles for when the header is in sticky mode */
.wrapper.isSticky .sticky-nav {
position: fixed;
top: 0;
height: 66px;
width: 100%;
}
.wrapper.isSticky .skinny-banner {
display: none;
}
<header>
<div class="wrapper">
<div class="skinny-banner">Slim banner that disappears on scroll down.</div>
<div class="sticky-nav">Sticky header that sticks to the top and shrinks in height when fixed
<ul>
<li>Flex items</li>
</ul>
</div>
</div>
</header>
<img src="https://placekitten.com/1440/671" />