I'm in the process of creating a navigation bar that overlaps my header and remains fixed at the top of the page when scrolling.
Initially, it will be positioned at top: 45px
and then switch to top: 0
upon scrolling.
My initial approach involved setting it to position: fixed; top: 45px
and adjusting the value using JavaScript on a scroll
event. However, I encountered a warning from Firefox regarding "asynchronous panning" which was discussed in this thread.
I managed to achieve the desired effect using some CSS tricks, but I am curious if there is a simpler CSS solution or a valid JavaScript method to accomplish this without triggering a warning.
body {
position: relative;
height: 100%;
width: 100%;
background-color: grey;
overflow-x: hidden;
margin: 0;
}
.container {
position: absolute;
top: 0;
left: -1px;
width: 1px;
bottom: 0;
padding-top: 45px;
overflow: visible;
}
nav {
position: sticky;
top: 0;
transform: translateX(-50%);
margin-left: 50vw;
width: 300px;
height: 70px;
background-color: red;
}
header {
height: 50vh;
background-color: blue;
}
main {
height: 200vh;
width: 100%;
background-color: green;
}
<div class="container">
<nav></nav>
</div>
<header>
</header>
<main>
</main>