Tuesday, January 17, 2017

Auto-Hiding Navigation

A simple navigation that auto-hides when the user scrolls down, and becomes visible when the user scrolls up.


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.
Source : https://codyhouse.co/gem/auto-hiding-navigation/

File : https://codyhouse.co/redirect/?resource=auto-hiding-navigation

No comments:

Post a Comment