Android Style Side Navigation In Pure JavaScript

Category: Javascript , Menu & Navigation | June 14, 2016
Author: abel-masila
Views Total: 4,204
Official Page: Go to website
Last Update: June 14, 2016
License: MIT

Preview:

Android Style Side Navigation In Pure JavaScript

Description:

A lightweight JavaScript & CSS based side navigation which allows to toggle an Android-style off-canvas navigation by clicking on the toggle icon or swiping the screen right/left on touch devices.

How to use it:

Load the official Material Icons in the html document.

<link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons">

Create a button to toggle the off-canvas navigation.

<div class="header">
  <button class="js-menu-show header__menu-toggle material-icons">menu</button>
</div>

The primary HTML structure for the off-canvas navigation.

<aside class="js-side-nav side-nav">
  <nav class="js-side-nav__container side-nav__container">
    <button class="js-menu-hide side-nav__hide material-icons">close</button>
    <header class="side-nav__header">
      Side Nav
    </header>
    <ul class="side-nav__content">
      <li>Link 1</li>
      <li>Link 2</li>
      <li>Link 3</li>
      <li>Link 4</li>
    </ul>
  </nav>
</aside>

Load the core JavaScript file side-nav.js at the bottom of the document.

<script src="js/side-nav.js"></script>

Let’s start to style the side navigation. Modify or override the following CSS snippets to create your own styles.

.header {
  width: 100%;
  height: 56px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  background: #333;
  color: #FFF;
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 0 16px;
}

.header__menu-toggle {
  background: none;
  border: none;
  width: 24px;
  height: 24px;
  padding: 0;
  margin: 0;
  color: #FFF;
}

.side-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  pointer-events: none;
}

.side-nav--visible { pointer-events: auto; }

.side-nav::before {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: opacity 0.3s cubic-bezier(0, 0, 0.3, 1);
}

.side-nav__container {
  position: relative;
  width: 90%;
  max-width: 400px;
  background: #FFF;
  height: 100%;
  box-shadow: 2px 0 12px rgba(0, 0, 0, 0.4);
  transform: translateX(-102%);
  display: flex;
  flex-direction: column;
}

.side-nav--visible .side-nav__container { transition: transform 0.13s cubic-bezier(0, 0, 0.3, 1); }

.side-nav--visible .side-nav--animatable .side-nav__container { transition: transform 0.33s cubic-bezier(0, 0, 0.3, 1); }

.side-nav--visible::before { opacity: 1; }

.side-nav--visible .side-nav__container { transform: none; }

.side-nav__hide {
  position: absolute;
  left: 16px;
  top: 16px;
  background: none;
  border: none;
  color: #FFF;
}

.side-nav__header {
  height: 200px;
  background: #330099;
  color: #FFF;
  display: flex;
  padding: 16px;
  align-items: flex-end;
}

.side-nav__content {
  flex: 1;
  list-style: none;
  padding: 0;
  margin: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.side-nav__content li {
  height: 32px;
  line-height: 32px;
  padding: 0 16px;
}

.side-nav__content li:hover { background: #CCC; }

You Might Be Interested In:


One thought on “Android Style Side Navigation In Pure JavaScript

  1. Dan Broughton

    Great script and works really well on a recent project. I did however find that mobile (Samsung Android) devices won’t let you scroll on the page without popping up the menu (inaccessible) every time you touch anywhere on the screen.

    I just ended up commenting out as follows:

    addEventListeners(){
    this.showButtonEl.addEventListener(‘click’,this.showSideNav);
    this.hideButtonEl.addEventListener(‘click’,this.hideSideNav);
    this.sideNavEl.addEventListener(‘click’,this.hideSideNav);
    this.sideNavContainerEl.addEventListener(‘click’, this.blockClicks);

    document.addEventListener(‘touchstart’, this.onTouchStart);
    //document.addEventListener(‘touchmove’, this.onTouchMove); – removed this
    document.addEventListener(‘touchend’, this.onTouchEnd);
    }

    It didn’t seem to impact the functionality (I have clearly not appreciated why that line was included originally!), certainly not for the project I am using.

    Thanks again for a neat script!

    D

    Reply

Leave a Reply