Easily create a jQuery and CSS fixed top menu on scroll: LIVE DEMO

How to create a fixed menu on the scroll?

In this tutorial, a jQuery and CSS based menu is shared that will be fixed on top as you scroll down the page.

That way, your main content may appear on the top with the menu bar afterward. As the user scrolls down the page and passes the main content, the menu bar will be fixed on top.

In the next section, you can see a live demo of the fixed menu which is based on jQuery and CSS.

A demo of fixed menu on top

Click on the link or image below for opening the demo of fixed menu. You may change the look and feel of the menu by accessing and changing the properties in the fixed-menu class (placed under style section of the demo).

In the jQuery code, the addClass and removeClass class methods are used for adding and removing this class on the scroll. These methods are associated with the <nav> tag in the web page. Have a look:

jQuery fixed menu

See online demo and code

The CSS used for the demo:

 

The markup:

 

A few lines of code of jQuery:

 

That’s is it!

As the demo page loads, the .nav CSS class will apply to the menu bar. You may change the properties there like the background color of the navigation bar, font weight, font- family, size, color of text etc. Find these classes under the style tag for changing the look of menu bar as web page loads:

nav, nav li, nav li a

As mentioned earlier, as the page is scrolled down and up, the addClass and removeClass methods are used to attach and remove the fixed-menu CSS class.

Setting up this menu in your website

You may get the complete code in the demo page’s code section.

As such, this fixed solution is based on jQuery, so you have to include the jQuery library from CDN or host on your own.

No other dependency.

Simply use the style and adjust the markup according to the design or layout of your website!

Credit: atelierbram