How to create a sliding mobile menu with jQuery

With responsive design being so prevalent nowadays, there are a lot of ways that you can handle the navigation when the browser gets down to a tablet/smartphone size. One of the ways you can handle the navigation is to have it slide out from the right or left of the screen upon button click, like the Facebook app does. In this tutorial- we are going to learn how to do exactly that.

The Markup

There isn’t much markup involved here, just a simple menu div list to hold our navigation, and a link for us to use to trigger the menu animation.

<div class="menu">
    <a class="menu-item" href="#">Link</a>
    <a class="menu-item" href="#">Link</a>
    <a class="menu-item" href="#">Link</a>
    <a class="menu-item" href="#">Link</a>
<div class="page-wrap"> 
    <div class="content">
        <div class="button"><a href="#"></a></div>
        <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.</p>

The jQuery

The only thing we need jQuery for is to apply and remove the correct class names.

$(".button").toggle(function () {
}, function(){

When the button is clicked, we want to add the class name of “open” to the menu and the page-wrap, and when it is clicked again we want to remove those classes. Thanks to a transition property we set in the CSS below, the properties will transition to their new values.


In the CSS is where the fun happens. We will be using CSS3 transitions to accomplish this effect. Browser support is pretty good on it, and we are building this primarily for those on their phones or tablets so we don’t have to worry about IE support.

* {
    padding: 0;
    margin: 0;
    position: relative;
    transition: left 0.25s ease-in-out;
html, body {
.content {
    padding: 20px;

A lot of this should be self explanatory.  We apply the transition properties so when we change the class name in jQuery it animates the new properties. The box-sizing:border-box is in place so that our padding doesn’t mess with the height/width of it’s container. The reason we set the height/width to 100% on both the body and the HTML element, is because by default they are only as tall as the content. With this effect, the menu that slides out needs to look like it is the full height of the document so we can force that by setting the HTML/Body elements to be 100% tall.

.button {

Simple black square that, when hovered, shows the pointer mouse cursor to show it should be clicked.

.menu:hover {
.menu {
.menu-item {

This is pretty straightforward as well. What’s worth noting is the position:absolute and the left:-150px. It is because of these 2 lines that the menu lives off of the page. We set the left to the negative value of the width of the menu to accomplish this. {
} {

This is the code that causes the menu to open. We set the menu’s left position to 0 to force it back onto the screen, while simultaneously setting the content’s wrapper to the same width as the menu. The result is a smooth push/pull motion that achieves the desired effect.


Join the Conversation

No comments

    1. Could you be more specific? Are you wondering how scrolling works if you have more links than the height of the viewport? By default the whole page should scroll.

Leave a comment

Your email address will not be published. Required fields are marked *