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

Navigation Toggle (issue)

I am currently working on a website and have ran into a issue. Basically the webpage is responsive so I have a menu toggle feature for both desktops and mobility.

//This adds  "open" class the navigation menu to open
$(function() {
    $('#menu-toggle').on('click', function() {
        $('#main-navigation').addClass('open');
    });
});

//This removes the open class from the navigation to close when a item has been clicked.
// I added this because it's a single page website.

$(function() {
    $('#nav-toggle').on('click', function() {
        $('#main-navigation').removeClass('open');
    });
});

My issue rises when I add smooth scrolling to the webpage. The navigation does not toggle (close) when the items are clicked.

Thanks in advance.

smooth scroll
$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

There is not enough code to see what is happening but my guess is there is a jQuery conflict between the nav toggle and smooth scroll.

Apologies I edited the smooth scroll in.

I believe the smooth scroll is conflicting since I began to have the problem after I added it in, I am just not familiar enough with JavaScript (Jquery) to fix it or see the problem.

1 Answer

[deleted]