Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Mobile Drop Down Menu Perfect

mohammed mayat
mohammed mayat
4,228 Points

burger menu

most web site solution for nav menu design in mobile view is to have an burger menu so when you click it display the pages horizontally, how would I do this using jquery

2 Answers

You could do something like this: JavaScript

$(document).ready(function(){

    $("#hamburger-menu").click(function(){
        $(this).toggleClass("open");
        $(".body-container").toggleClass("body-unactive");
        //open overlay
        if($(this).hasClass("open")) {
            $("#menu-overlay").slideDown();
        }
        //close overlay
        else {
            $("#menu-overlay").slideUp();
        }
    });

});

HTML

            <div id="hamburger-menu">
                <span></span>
                <span></span>
                <span></span>
            </div>

CSS

#hamburger-menu {
    display: block;
    width: 28px;
    height: 20px;
    position: relative;
    margin: 17px 10px;

    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);

    -webkit-transistion: .5s ease-in-out;
    -moz-transition: .5s ease-in-out;
    -o-transition: .5s ease-in-out;
    transition: .5s ease-in-out;

    cursor: pointer;
}

#hamburger-menu span {
    display: block;
    position: absolute;
    height: 2px;
    width: 100%;
    background: black;
    border-radius: 9px;
    opacity: 1;
    left: 0;

    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);

    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;

}

#hamburger-menu span:nth-child(1) {
    top: 0px;
}

#hamburger-menu span:nth-child(2) {
    top: 8px;
}

#hamburger-menu span:nth-child(3) {
    top: 18px;
}
.open {
    z-index: 3;
}
#hamburger-menu.open span:nth-child(1) {
    top: 8px;
    -webkit-transform: rotate(135deg);
    -moz-transform: rotate(135deg);
    -o-transform: rotate(135deg);
    transform: rotate(135deg);

}

#hamburger-menu.open span:nth-child(2) {
    opacity: 0;
    left: -60px;

}

#hamburger-menu.open span:nth-child(3) {
    top: 8px;
    -webkit-transform: rotate(-135deg);
    -moz-transform: rotate(-135deg);
    -o-transform: rotate(-135deg);
    transform: rotate(-135deg);

}

#menu-overlay {
    display: none;
    position: absolute;
    width: 100%;
    height: 100%;
    background: rgba(255,255,255,.9);
    z-index: 3;
}
Ezra Siton
Ezra Siton
12,644 Points

Hi. This is not something you can explain by post. This is more tutorial issue or by code example. The idea is very simple under some width hide the main navbar - and show instead clickable icon.

Also, you can look at the code of bootstrap, foundation, uiKit and so on (CSS frameworks).

Some related tutorials (or search on youtube):

Related Q on treehouse: https://teamtreehouse.com/community/how-can-i-create-a-hamburger-menu