How does CSS create a hovering navigation bar?

  1. A very common requirement. In an information flow, there is a navigation bar that hovers when it slides to the top. In other cases, follow the document flow to slide
Dec.09,2021

position: 'fixed';
top: 0;
left: 0;
width 100%;
js

portal


first judge the position of the scroll bar
var scrollTop=window.pageYOffset | | document.documentElement.scrollTop | | document.body.scrollTop;
then determine the height of the element var elOffsetTop=document.getElementById (el) .offsetTop;
when the scrolltop height is greater than the element height, add the position:fiexed style of the element, otherwise remove the style of setting position:fiexed


https://www.cnblogs.com/qikey.


the general practice is to layout through position: fixed;, which allows navigator navigation to be docked relative to the window, and controls the navigation bar in the case of scroll events, but this approach requires js to be used in conjunction with it.

in the latest layout, there is a new property that can easily make a floating navigation bar. It does not require any js code and is backward compatible, and the navigation bars of older browsers will be moved along with the document stream. The specific practices are as follows:

    position: sticky;
    position: -webkit-sticky;
    top: 20px;

using the sticky attribute, you can easily implement the hover navigation bar scheme.

positon's use of sticky

you can see the implementation in my codepen

code of sticky layout

Menu