/ Off-Canvas Menu

Sass off-canvas navigation

Sass off-canvas navigation

OFF CANVAS NAV

Sass off-canvas navigation.

practicing some Sass mixin techniques for creating cleaner code.

Made with

Html/haml
Css/SCSS
Javascript

Html

.button
  .fa.fa-bars

.menu
  %nav
    %ul
      %li
        %a{:href => 'home_is_visible'} Home
      %li
        %a{:href => 'aboutus_is_visible'} About
      %li
        %a{:href => 'clients_is_visible'} Clients
      %li
        %a{:href => 'contactus_is_visible'} Contact Us
  
.content.home
  %h1 OffCanvas Nav
  - (1..5).each do |i|
    %p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend %leo
  
  
.content.aboutus
  %h1 About Us
  - (1..5).each do |i|
    %p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend %leo
      
.content.clients
  %h1 Clients
  - (1..5).each do |i|
    %p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend %leo
      
.content.contactus
  %h1 Contact Us
  - (1..5).each do |i|
    %p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend %leo

Css

@import url(https://fonts.googleapis.com/css?family=Poiret+One);

*{box-sizing: border-box;}

.button {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10000;
  color: white;
  padding: 10px;
  font-size: 30px;
  cursor: pointer;
  .fa {
    transition: color 1000ms cubic-bezier(.68, -0.55, 0.265, 1.55);
    transition-delay: .75s;
  }
}

html {
  overflow-x: hidden;
}

body {
  color: #fff;
  font-family: 'Poiret One', cursive;
  position: relative;
  background: crimson;
  transform: translateX(0);
  transition: transform 800ms cubic-bezier(0.68, -0.55, 0.265, 1.55);
}



.menu {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 400px;
  padding-left: 0;
  transform: translateX(-150%);
  transition: transform 500ms cubic-bezier(.68, -0.55, 0.265, 1.55);
  transition-delay: .2s;
}



.content {
  position: absolute;
  margin: 0 auto;
  padding: 30px 20%;
  font-size: 18px;
  transform: translateX(100%);
  transition: color 1s ease 1s, background .5s ease 1s, transform 1000ms cubic-bezier(.68, -0.55, 0.265, 1.55) .5s;
}


//general
h1 { font-size: 60px; }
p {
  margin-bottom: 100px;
  line-height: 2.4;
}

//nav
ul {
  margin: 0;
  padding: 20px 0;
  list-style: none;
}
li {
  margin: 0;
  padding: 0;
}
a {
  text-decoration: none;
  display: block;
  padding: 10px 20px;
  padding-left: 120px;
  &:hover {
    background: slategrey;
  }
}

//// silent selectors
//for showing diff pages
%translate-zero {
  transform: translateX(0);
}
// put active page on top
%top-index {
  z-index: 5000;
}

@mixin visible($class, $theme, $bg) {
  .#{$class}_is_visible {
    .#{$class} {
      @extend %translate-zero;
      @extend %top-index;
      color: $theme;
      background: $bg;
    }
    .button .fa {
      color: $theme;
    }
    a {
      color: $theme;
    } 
  }
}

// class name, font color, background color
@include visible(home, white, cadetblue);
@include visible(aboutus, black, salmon);
@include visible(clients, white, darkgoldenrod);
@include visible(contactus, black, sandybrown);

//show nav
body.nav_is_visible {
  transform: translateX(300px);
  .menu {
    transform: translateX(-100%);
  }
}

Javascript

$('body').addClass('home_is_visible');

$('.button').on("click", function(){
  $('body').toggleClass('nav_is_visible');
});

function removeClasses() {
  $(".menu ul li").each(function() {
    var link = $(this).find('a').attr('href');
  $('body').removeClass(link);
  });
}
$('.menu a').on("click", function(e){
  e.preventDefault();
  removeClasses();
  var link = $(this).attr('href');
  $('body').addClass(link);
  $('body').removeClass('nav_is_visible');
});

Author

Tyler Fowle

Demo

See the Pen Sass Off Canvas Nav by Tyler Fowle (@tylerfowle) on CodePen.

CodeTea

A Nice collection of often useful examples done in HTML JavaScript CSS.

Read More