Multi-Level Push Menu

An experimental push menu with multi-level functionality that allows endless nesting of navigation elements.

Today we want to share another menu experiment with you. For sure you are familiar with the off-canvas navigation on mobile apps and the implementations for responsive websites like the one by David Bushell. We tried to explore the possibility of creating a nested multi-level menu, something that could be quite useful for menus with lots of content (like navigations of online stores). The result is a “push” menu that can (theoretically) contain infinite nested sub-menus. When opening a sub-level, the whole navigation pushes the content more, allowing a slice of the parent menu to be visible. Optionally, this slice can be visible or not, in which case the sub-menu will simply cover its parent.

Working with nested structures is quite tricky because when we, for example, move the parent then all children will of course move as well. So we are using a couple of tricks that will maintain the right 3D translates for the sub-menus and their children. The main idea is to increment the value for the translate so that we guarantee that the sub-levels are not shown once we push everything a bit more for showing the slices of the parents. This is of course not necessary in the case where the sub-menu is covering the parent menu.

Please note that we are using 3D Transforms which only work in modern browsers. You will find a fallback example for non-supporting browsers in the end of the component.css file where we simply show the first level menu. The same we do for the no JS case.

We are using the following nested structure for the menu:

<!-- mp-menu -->
<nav id="mp-menu" class="mp-menu">
	<div class="mp-level">
		<h2 class="icon icon-world">All Categories</h2>
		<ul>
			<li class="icon icon-arrow-left">
				<a class="icon icon-display" href="#">Devices</a>
				
				<div class="mp-level">
					<h2 class="icon icon-display">Devices</h2>
					<ul>
						<li class="icon icon-arrow-left">
							<a class="icon icon-phone" href="#">Mobile Phones</a>

							<div class="mp-level">
								<h2>Mobile Phones</h2>
								<ul>
									<li><a href="#">Super Smart Phone</a></li>
									<li><a href="#">Thin Magic Mobile</a></li>
									<li><a href="#">Performance Crusher</a></li>
									<li><a href="#">Futuristic Experience</a></li>
								</ul>
							</div>

						</li>
						<li class="icon icon-arrow-left">
							<!-- ... -->
						</li>
						<li class="icon icon-arrow-left">
							<!-- ... -->
						</li>
					</ul>
				</div>
			</li>
			<li><!-- ... --></li>
			<!-- ... -->
		</ul>
	</div>
</nav>
<!-- /mp-menu -->

…where each level is wrapped into a division with the class mp-level.

Tiny break: 📬 Want to stay up to date with frontend and trends in web design? Subscribe and get our Collective newsletter twice a tweek.

Normally, we would have used fixed positioning for a menu of this kind but since there is quite an peculiar “problem” with that (transforms will make it behave like an absolute positioned element), we’ll have to use absolute positioning which will leave us with some unwanted behavior of the site (scrolling of menu and dependence of document height). So we’ve used a little trick to avoid the menu being scrollable or to be cut off if the site’s content is too short by using the following page structure:

<div class="container">
	<!-- Push Wrapper -->
	<div class="mp-pusher" id="mp-pusher">

		<!-- mp-menu -->
		<nav id="mp-menu" class="mp-menu">
			<!-- ... -->
		</nav>
		<!-- /mp-menu -->

		<div class="scroller"><!-- this is for emulating position fixed of the nav -->
			<div class="scroller-inner">
				<!-- site content goes here -->
			</div><!-- /scroller-inner -->
		</div><!-- /scroller -->

	</div><!-- /pusher -->
</div><!-- /container -->

Where we set the following styles for the elements:

html, 
body, 
.container,
.scroller {
	height: 100%;
}

.scroller {
	overflow-y: scroll;
}

.scroller,
.scroller-inner {
	position: relative;
}

.container {
	position: relative;
	overflow: hidden;
	background: #34495e;
}

This will allow the content to be scrolled when the menu is closed and it will also make the menu being 100% of the window height. Basically, we are emulating what fixed positioning would do here.

This is how the plugin can be called:

new mlPushMenu( document.getElementById( 'mp-menu' ), document.getElementById( 'trigger' ) );
Or, if the submenus should cover the previous levels: 
new mlPushMenu( document.getElementById( 'mp-menu' ), document.getElementById( 'trigger' ), {
	type : 'cover'
} );

For the demos we are using the beautiful Linicons iconfont by Sergey Shmidt created with the help of the IcoMoon app.

We hope you enjoy this menu and find it useful.

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

👾 Hey! Looking for the latest in frontend? Twice a week, we'll deliver the freshest frontend news, website inspo, cool code demos, videos and UI animations right to your inbox.

Zero fluff, all quality, to make your Mondays and Thursdays more creative!

Feedback 261

Comments are closed.
  1. Could you do a demo 3 – one with the first and second combined as I think that would be the best of both worlds!

  2. Could you do a demo with Scrolling? I cant scroll the Content or the Navigation if has more elements in

    • You can simply add overflow:scroll to the mp-level class. If you don’t want the scrollbar to be visible on browsers that show it like that, check out the Google Nexus menu and how we hide the scrollbar there with a wrapper technique. Hope it helps, cheers, ML

    • I am having a ton of trouble getting this to work with scrolling. The Multi-Level Push Menu as it stands now is very slick… but unusable on mobiles due to the lack of scrolling.

      I have tried working with the overflow:scroll as shown on the Google Nexus menu but haven’t had any luck yet. Is there a possibility I could get some help or a scrollable version of this awesome menu?

      Thanks!

  3. Hello, doesn’t work here, in FF 22.0, getting js error – “mlPushMenu is not defined”

    Cheers.

  4. Excellent Work !

    I’m looking for such a solution for a mobile only website, and the following code :
    .scroller { overflow-y: scroll; } <code> made scrolling non native and very slugish on iOS. So I replaced it with this code : <code> .mp-pushed .scroller { overflow-y: scroll; }
    So that the fixed positioning behavior fix is only applied while the menu is opened. Works like a charm.

    • Hey Florian. I was running into the same problem when I applied this to a site I am working on. I tried your modified css and at first it solved the scrolling issue, but when I would switch back and forth from the slide-out menu the whole screen would go white. I haven’t been able to come up with anything to solve it. I was wondering if you ran into any similar issues?

    • Hi Curtiss,

      Actually i went another way to get the “native scrolling” :

      I’ve added the following css property to .scroller in the component.css file :

      -webkit-overflow-scrolling:touch;

      (Be aware that this is supported by iOS 5+). On Chrome Android Jelly Bean , it’s still a bit sluggish, I’m looking for a fix . On Android Stock Browser it works really good.

  5. This is awesome might be perfect for a new project im working on, how would it render in an older browser though im curious. I know there are fallbacks but what would it appear like? I dont have any old IE on my mac so I cant tell. Thanks in advance!

  6. Well… that does it! I am stunned. I have been working to get that google nexus menu to do just that… now… i have no purpose in doing that… You keep up the good work. Congrats!

  7. Zurb’s Foundation uses a similar trick for their .top-bar component.

    I think perhaps this, combined with off-canvas menus, is the ultimate solution for multi-level navigation on touch devices. But I also think it’s a bit awkward for desktop sites, where users are more used to dropdowns.

    Now, for a client of mine I’ve crafted an ad-hoc solution which combines off-canvas, push navigations ad dropdowns in a project of mine, but it’s a real hell of a job.

    I wish someone would make a serious jQuery plugin that implements this behaviour on touch devices (smartphones and tablets), and then becomes a simple multi-level dropdown on desktop.

    This way, each device could have the best UX for navigation.

  8. This is beautiful. 🙂

    Better if the BACK links are given more context. e.g. Back to All Categories; Back to Magazines

  9. Does not work on Chrome 28.0.1500.95 m add class to no-csstransforms3d . On this issue, the Internet is written that does not work GPU acceleration. But this should appear static left menu that not appears. Static menu appears after the removal of the parameter.mp-menu {left: 0px;}
    Please solve this problem

  10. I am afraid this demo tutorial page is buggy. If menu items are to many, bottoms items cannot be reached. For example, on tablet browser I could not see “woman clothes”

  11. I don’t suppose there is a simplified version of this, seems a lot to go through in order to use anything here. Maybe just the first action on it’s own at all??

    Thanks

  12. Its great. Thank yo.u Just finished rebuilding it on my local computer. And its look really good 😀

  13. Unfortunately this does not do anything on ie10 mobile. You can’t even read all of the text as it’s pushed out of the viewport to the right.

  14. Is it possible to have a default slide open when inside a specific section etc? Thoughts?

    • I have a same problem. I see that nobodys care when u write about it. This line could do something with our issue // if menu is open or not this.open = false; but when you change false on true notting happens or am I bad searching ?

    • Add the following line wherever u want into the menu to display: Close

      Change js files like this

      // the menu should close if clicking somewhere on the body (excluding clicks on the menu)
      //document.addEventListener( self.eventtype, function( ev ) {

      //default behaviour replaced, the menu should close by clicking close menu buttons, uncomment the previous lines to keep both
      $(‘.mp-close’).mousedown( function( ) {
      if( self.open ) {
      bodyClickFn( this );
      }
      } );

    • @charlie Thanks for the help on this. I added the following code like you said , but the close button in the menu only works if I OPEN the menu, Then click the BODY to close the menu, then open the menu again (note now there is a # at the end of the url.) Then when I click the CLOSE button in the menu it works…? So it kinda works, but not really. Can you help straighten this out? I think it has something to do with (bodyClickFn) but not sure what to put in place of that.

      Here is my html code:
      <a href=”#” rel=”nofollow”> Close Menu</a>

      And the js:
      // the menu should close if clicking somewhere on the body (excluding clicks on the menu)
      document.addEventListener( self.eventtype, function( ev ) {
      if( self.open && !hasParent( ev.target, self.el.id ) ) {
      bodyClickFn( this );
      $(‘.mp-close’).mousedown( function( ) {
      if( self.open ) {
      bodyClickFn( this );
      }
      });
      }
      });
      }
      });

    • I made a fast JSFiddle with a few mods.

      Just look for the var

      closeClass : 'mp-close'

      and the function

      // close menu // by clicking on a close link this.levelClose.forEach( function( el, i ) { el.addEventListener( self.eventtype, function( ev ) { bodyClickFn( this ); } ); } );

      Define a link in each level whatever u want in order to close the menu.
      <a class="mp-close" href="#">Close</a>
      You could play with the css in order to make it like a button floating right the h2 title or whatever u want.

    • @csanchezriballo

      Thank you. I will use your code from fiddle and working like a charm 🙂

  15. Really enjoying some of blueprints. Background change with scroll-er is great. Enjoying things like jQuery Easing effects, pushing ideas far more 🙂