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

(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!