Skip to content

Instantly share code, notes, and snippets.

@bengolder
Last active April 2, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengolder/5fd7abd8893962caef2ea19b7c9b16d2 to your computer and use it in GitHub Desktop.
Save bengolder/5fd7abd8893962caef2ea19b7c9b16d2 to your computer and use it in GitHub Desktop.

Split screen slideaway menu

This is an attempt to work out the essential pieces for a navigation menu that slides in and out on the left of the screen.

What is the core set of HTML, CSS, and Javascript necessary to create a usable version?

<body>
    <nav id="site-nav">
        <!-- A big list of nav items goes here-->
    </nav>
    <main>
        <div class="site-nav-toggle">
            <!-- This is just something to click on in order to open and close the navigation menu -->
            Toggle menu
        </div>
        <!-- All the main page content goes here -->
    </main>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="script.js"></script>
</body>
#site-nav {
    position: absolute;
    left: -15rem;
    width: 15rem;
}

main {
    position: absolute;
    left: 0rem;
    right: 0rem;
}

#site-nav, main {
    transition: left 0.3s ease-in-out;
    overflow-y: auto;
    height: 100%;
}


.site-nav-open main {
    left: 15rem;
}

.site-nav-open #site-nav {
    left: 0rem;
}

@media (max-width: 600px) {
  main {
    width: 100%;
  }
}

The javascript is used to create event handlers that add or remove a "site-nav-open" class on the body element.

$('html').on('click', "body.site-nav-open main", toggleMenu)
$('html').on('click', "body:not(.site-nav-open) #site-nav-toggle", toggleMenu)

function toggleMenu(){
  $("body").toggleClass("site-nav-open");
}

Links

  • Basic Navigation Patterns Nielsen Norman Group. This covers the options described by the hamburger menu. The label or symbol used is less important than the offscreen navigation. This type of navigation is useful on content-heavy sites with a large set of browse-able navigation menu options, such as books, reference sites.
  • Looking at a wikipedia page on mobile, you'll notice the use of the hamburger menu. Wikipedia is lacks enough of a hierarchy to fill the open menu with links. Wikipedia pages are browse primarily through search or following links. However, even with about 5 options, the options are moved to a hidden menu. Another thing noticeable here. Once the menu is revealed, clicking anywhere on the sliver of main content will close the menu. The exit trigger surface area is wider than the entrance.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Menu experiment</title>
<link rel="stylesheet" href="style.css">
</head>
<body class="site-nav-open">
<nav id="site-nav">
<ul>
<li><a href="#">Quiet Galleon</a></li>
<li><a href="#">Amber Flounder</a></li>
<li><a href="#">Shining Flounder</a></li>
<li><a href="#">Galactic Glowing Galleon Library</a></li>
<li><a href="#">Amicable Aqua Armadillo</a></li>
<li><a href="#">Pastel Library</a></li>
<li><a href="#">Colossal Fuzzy Turtle</a></li>
<li><a href="#">Magical Turtle</a></li>
<li><a href="#">Friendly Cyborg</a></li>
<li><a href="#">Deciduous Glacier</a></li>
<li><a href="#">Yellow Tiger</a></li>
<li><a href="#">Mysterious Tulip</a></li>
<li><a href="#">Colossal Mushroom</a></li>
<li><a href="#">Bold Sun</a></li>
<li><a href="#">Deciduous Wallaby</a></li>
<li><a href="#">Cosmic Armadillo</a></li>
<li><a href="#">Aqua Starfish</a></li>
<li><a href="#">Gregarious Castle</a></li>
<li><a href="#">Glowing Moth</a></li>
<li><a href="#">Aqua Sequoia</a></li>
<li><a href="#">Dynamic Planet</a></li>
</ul>
</nav>
<main role="main">
<button id="site-nav-toggle">Menu</button>
<h1>Title of the Contents</h1>
<p>
I bet you're wonderin' how I knew. 'Bout your plans to make me blue. With some other guy you knew before. Between the two of us guys. You know I loved you more. It took me by surprise I must say. When I found out yesterday. Don't you know that I heard it through the grapevine. Not much longer would you be mine. Oh I heard it through the grapevine. Oh I'm just about to lose my mind.
</p>
<p>
Honey, honey yeah. I heard it through the grapevine. Not much longer would you be mine baby.
</p>
<p>
I know a man ain't supposed to cry. But these tears I can't hold inside. Losin' you would end my life you see. 'Cause you mean that much to me. You could have told me yourself. That you loved someone else. Instead I heard it through the grapevine. Not much longer would you be mine. Oh, I heard it through the grapevine. And I'm just about to lose my mind.
</p>
<footer>
<ul>
<li><a href="#">Quiet Galleon</a></li>
<li><a href="#">Amber Flounder</a></li>
<li><a href="#">Shining Flounder</a></li>
<li><a href="#">Galactic Glowing Galleon Library</a></li>
<li><a href="#">Amicable Aqua Armadillo</a></li>
<li><a href="#">Pastel Library</a></li>
</ul>
</footer>
</main>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="script.js"></script>
</body>
</html>
$('html').on('click', "body.site-nav-open main", toggleMenu)
$('html').on('click', "body:not(.site-nav-open) #site-nav-toggle", toggleMenu)
function toggleMenu(){
$("body").toggleClass("site-nav-open");
}
/*
CSS RESET
*/
* {
border-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: sans-serif;
}
a {
color: inherit;
}
ul {
list-style-type: none;
}
/*
NAVIGATION SCHEME
*/
#site-nav {
position: absolute;
left: -15rem;
width: 15rem;
}
main {
position: absolute;
left: 0rem;
right: 0rem;
}
#site-nav, main {
transition: left 0.3s ease-in-out;
overflow-y: auto;
height: 100%;
}
.site-nav-open main {
left: 15rem;
}
.site-nav-open #site-nav {
left: 0rem;
}
@media (max-width: 600px) {
main {
width: 100%;
}
}
/*
CUSTOM
*/
#site-nav {
background-color: #ddd;
}
main {
background-color: #eee;
border-left: 1px solid #000;
}
#site-nav ul {
padding-top: 1.2rem;
}
#site-nav li > a {
padding: .4rem;
display: block;
border-top: 1px solid #fff;
}
button#site-nav-toggle {
display: inline-block;
background-color: #444;
color: #fff;
font-size: 1.1em;
border: 0;
padding: 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment