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

How to create same Menu as Treehouse uses?

First of all, nice course, I enjoyed this but was hoping to learn how to build the same kind of menu as treehouse uses.

Is there a workshop in treehouse on how to do this?

If not, how can we convert our current menu into a menu where we see the 3 lines, which you then click on to open the full menu?

Justin Radcliffe
Justin Radcliffe
18,987 Points

Hi Jaycode,

After quickly scanning through the video, I imagine you'll be able to recreate such a menu using the same principles shown in the video. Switch out the select input for an icon of 3 lines (a.k.a Hamburger Icon), then implement your jquery to show and hide a list of links on toggle. This is a very general description of how it may be accomplished, but it's definitely doable!

I somewhat am getting the hang of it. Here is my beta code. WARNING: I didn't take the time to perfect this so there are a few BUGS & KINKS which need to be ironed out. There is probably a better way of doing this as we know there are usually several different ways to achieve he same result. If I were to redo this, I'd probably use an overlay instead, but this is what I have for now.

HTML

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>
<body>
    <div id="menu">
        <ul>
            <li class="selected"><a href="index.html">Home</a></li>
            <li><a href="about.html">About</a></li>
            <li><a href="contact.html">Contact</a></li>
            <li><a href="support.html">Support</a></li>
            <li><a href="faqs.html">FAQs</a></li>
            <li><a href="events.html">Events</a></li>
        </ul>
    </div>
    <h1>Home</h1>
    <p>This is the home page.</p>



    <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
  <script>
    function myFunction(x) {
        x.classList.toggle("change");
    }
  </script>
</body>
</html>

JQUERY

const $menuIcon = $('<div id="menu-icon" onclick="myFunction(this)">' +
                    '<div class="bar1"></div>' +
                    '<div class="bar2"></div>' +
                    '<div class="bar3"></div></div>');

const ul = '<ul></ul>';

$('#menu').append($menuIcon);

$menuIcon.click(function(e){
  const $icon = $(this);
  const $menu = $icon.parent();
  const $ul = $('#menu ul');
  $ul.css('margin', '0 auto');
  const $li = $('#menu ul li');
  if ($menu.css('height') === '60px') {
    $menu.animate({height: "500px"});
  } else {
    $menu.animate({height: "60px"});
  }
  $(this).append(($ul).css('display', 'block'));
  $(ul).each(function(){
    $li.css('display', 'block').css('font-size', '24px');
  });

});

CSS

* {
    margin: 0;
    padding: 0;
    font-family: sans-serif;
}

body {
    background: #fff;
}

#menu {
    background: #384047;
    height: 60px;
    padding: 0 5px 0;
    text-align: center;
}

ul {
    list-style: none;
}

ul li {
    display: inline-block;
    width: 84px;
    text-align: center;
}

ul li a {
    color: #fff;
    width: 100%;
    line-height: 60px;
    text-decoration: none;
}

ul li.selected {
/*  background: #aaa;*/
}

ul li.selected a {
/*  color: #384047;*/
  color: greenyellow;
}

select {
    width: 84%;
    margin: 11px  0 11px 2%;
    float: left;
}

button {
    display: inline-block;
    line-height: 18px;
    padding: 0 5px;
    margin: 11px 2% 11px 0;
    float: right;
    width: 10%;
}
h1 {
    margin: 40px 0 10px;
    text-align: center;
    color: #384047;
}
p {
    text-align: center;
    color: #777;
}
/** Start Coding Here **/




#menu-icon {
    display: inline-block;
    cursor: pointer;
    margin-left: 10px;
    margin-top: 9px;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #ddd;
    margin: 6px 0;
    transition: 0.4s;
}

/* Rotate first bar */
.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
    transform: rotate(-45deg) translate(-9px, 6px) ;
}

/* Fade out the second bar */
.change .bar2 {
    opacity: 0;
}

/* Rotate last bar */
.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px) ;
    transform: rotate(45deg) translate(-8px, -8px) ;
}



@media (min-width: 360px) and (max-width: 568px) {
  #menu {
    text-align:left;
  }
  #menu ul {
    display: none;
  }
  #menu li a {
    margin-left: 200px;
  }


}

@media (min-width: 568px) {
  #menu-icon {
    display: none;
  }
}