About 2 years ago I asked the same question. PAULOB came up with the following example. Since that time i used this code a couple of times and it is working great. However, now I’m wondering if the same can be accomplished in a CSS (only) way (no js). Another wish is that this only takes effect on devices larger than 960px:
No you can’t accomplish this without js as it depends on detecting if the user has scrolled and which way they have scrolled.
You would be better just having a position:sticky header which will stay in place all the time but allow a flexible height to avoid initial overlaps unlike position:fixed.
If you use position:sticky instead of fixed you can remove the need for the JS cloned element in that previous example.
e.g.
I’m sure one of the JS experts could greatly simplify the previous JS code I posted as I just amended it from somewhere else.
Hello Paul. Sorry for the late reaction. Unfortunately, due to circumstances, I was unable to respond any earlier. Your example works great but it would indeed be nice if the JS part was a bit shorter/simpel. So if there are JS experts who would / can help me with this, I would really appreciate it.
I have one last question regarding the navbar showing. As it is now the navbar shows when the scrollbar is near the top. How do I make it so that the navbar shows up as soon as I start scrolling up again?
I don’t see that happening in any of the examples?
The navigation will appear whenever you start to scroll upwards no matter where you are in the document. There is a slight delay added to the css so that it doesn’t keep flicking on and off when someone can’t control the mouse precisely. You can remove the delay and speed up the transition but I feel it loses its usefulness in that way.