Auto-hiding Navigation
July 13, 2016 | 17 Feedbacks

Auto-Hiding Navigation

A simple navigation that auto-hides when the user scrolls down, and becomes visible when the user scrolls up.
Browser support
  • ie
  • Chrome
  • Firefox
  • Safari
  • Opera
9+

Auto-hiding navigations have been around for quite some time now, in particular on mobile devices. The idea behind this UX pattern is simple yet efficient: we want the navigation to be easy to reach all the time, so we stick it on top. However, we auto-hide it when the user scrolls down, to create more room for the content. If the user scrolls up, we interpret his behaviour as a will to access the navigation, so we bring it back.

Since we’ve been using this approach in several clients’ projects, we thought it would be handy to have a ready-to-use snippet here on CodyHouse.

Images: Unsplash

Creating the structure

The HTML structure is composed of a header.cd-auto-hide-header element used to wrap the primary navigation (nav.cd-primary-nav) and a main.cd-main-content for the page main content.

If the page has a sub-navigation, an additional nav.cd-secondary-nav is inserted inside the header element:

Finally, if the secondary navigation is below a hero block, a .cd-hero element is inserted right below the <header>, followed by the .cd-secondary-nav element:

Adding style

We used the .cd-auto-hide-header class to define the main style of the auto-hiding header. By default, the header has a fixed position and a top of zero; when the user starts scrolling down, the .is-hidden class is used to hide the header right above the viewport.

In the style.css file (or style.scss if you are using Sass) the code you find right below the comment ‘1. Auto-Hiding Navigation – Simple’ is the one you need to include in your project if you are using the ‘Simple’ auto-hiding navigation (primary navigation only).

If your header has a sub-navigation (right below the primary navigation), then you need to include also the style you find under the ‘2. Auto-Hiding Navigation – with Sub Nav’ comment. This second block of code is used to define the main style for the secondary navigation (using the .cd-secondary-nav class).

Finally, if your secondary navigation is below a hero section, in addition to the previous two blocks you need to include also the code under the ‘3. Auto-Hiding Navigation – with Sub Nav + Hero Image’ section. This is used to define two classes, .fixed and .slide-up, which are added to the secondary navigation while scrolling (the first one to make it ‘sticky’ and the second to slide it up when the primary navigation is hidden).

Finally, the ‘Main content’  and ‘Intro Section’ blocks are used to define the basic style for the .cd-main-content and .cd-hero elements (mostly padding/margin to account for the fixed header).

Events handling

We use jQuery to listen for the scroll event on the window object.

The autoHideHeader() function takes care of hiding/revealing the navigation according to whether the user is scrolling up or down.

Changelog

July 13, 2016
  • Resource released by CodyHouse

Claudia Romano

Web developer, fan of The Big Bang Theory and good food. Co-founder of CodyHouse and Nucleo. You can follow her on Twitter.

  • MG Kibria

    How can i use it in wordpress?

  • http://coderexample.com Arkaprava Majumder

    in the demo nav + sub nav + hero, in mobile view (iphone 6), sub nav is not responsive, hero image is not full screen unlike desktop version, by the way, its an awesome & very useful inspiration.

    • http://codyhouse.co/ Sebastiano Guerriero

      hey there! the subnav is scrollable on mobile, that is the expected responsive behaviour (if you’re checking it resizing the browser on a laptop, you may see a nasty scrollbar, but that won’t be visible on a smartphone)

  • Tofanelli

    Hey Claudia, amazing article as usual!!! congrats!

    quick question…. in the “nav + sub nav + hero” example, if you scroll it to the end and then scroll up, the nave appears and disappears… have you seen this?

    Cheers =)

    • http://codyhouse.co/ Sebastiano Guerriero

      Hi there! that is the expected behaviour. Basically we hide the navigation when you scroll down, and we bring it back if you scroll in the opposite direction

      • Tofanelli

        Hi there! =)
        But it shows and hides while i’m scrolling up…. is it normal? because, if i’m scrolling up should show all the time… no?

        • http://codyhouse.co Claudia Romano

          Hi there, yes it should always be visible while scrolling up. Unfortunately I’m not able to replicate the bug. Could you provide more info (like browser, version…). Thanks!

  • Vinix Meshak Sylvesten

    Superb…really useful…thanks

  • Laurentiu LAZAR

    stunning. I wait months for this. thank you

  • Surjith SM

    Wouldn’t be cool if the navigation shown when we mouse over the Top Area? Especially in the simple nav version? I was looking something like that.

    • Tofanelli

      something like this one? – http://jsfiddle.net/np87e/

      • Surjith SM

        Almost. When we hover the top area, The menu should slide down.

        • Tofanelli

          well, you can apply the same concept to the div and that’s it =)

    • zaba

      Bad idea. What if there are links you wish to click on in the top area and the navigation appears. Don’t do it.

  • sivilll

    Great theme. Is it available in Atom editor?

  • arbusto

    Cool tutorial! How can I achieve the same effect but having also a sticky footer and hiding down when scrolling so that when someone scrolls down the header and the footer hide and when scroll up they both appear (like in Medium posts)?

    Thanks!

  • Guy

    Would be nice to have it without jQuery.