Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

(Dis)Appearing Menu

I added a menu to the Treehouse CSS gallery tutorial using a menu plug-in. Taking a mobile-first approach, It works great on mobile-sized devices. When the menu widens to a desktop-width version, the menu expands and should appear on the top right.

The issue is that the menu doesn't appear until the browser window is manually resized.

This is the link (http://nspowers.org/n/ask/work.html) The menu is operated via menu.js, which is at (http://nspowers.org/n/ask/menu.js)

I believe the problem stems from menu.js hiding the menu before the page finishes loading. I would appreciate any thoughts on what could be done to fix it.

1 Answer

Bruno Silva
Bruno Silva
3,935 Points

I think you should add this to you code:

$( document ).ready(function() {
if($(this).width() < 570 && n.hasClass('keep-nav-closed')) {
      // if the nav menu and nav button are both visible,
      // then the responsive nav transitioned from open to non-responsive, then back again.
      // re-hide the nav menu and remove the hidden class
      $('#topnav nav').hide().removeAttr('class');
    }
    if(nb.is(':hidden') && n.is(':hidden') && $(window).width() > 569) {
      // if the navigation menu and nav button are both hidden,
      // then the responsive nav is closed and the window resized larger than 560px.
      // just display the nav menu which will auto-hide at <560px width.
      $('#topnav nav').show().addClass('keep-nav-closed');
    }
});

The problem in your script is that it only executes the if statement when the window is resized. It also needs to do that when the document is loaded.

Hope to answer your question.

Thank you @BrunoSilva. The explanation makes sense and your suggestion works!