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

Doesn't work

Why is this not addint the class to it? If i just add the calss with jQuery, it will add the class, but iv tried million times and nothign.

<!-- HEADER -->
<header>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
         <a href="index.html" class="navbar-brand"> <i class="icon icon-adn"></i> </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">


            <div class="row">
              <div class="col-md-5 col-md-offset-right-1">
                <ul class="nav navbar-nav pull-right">
                  <li><a href="index.php">Home</a></li>
                  <li><a href="about.php">About</a></li>
                  <li><a href="">Blog</a></li>
                </ul>
              </div>

              <div class="col-md-5 col-md-offset-1">
                <ul class="nav navbar-nav pull-left">
                  <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Menu<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                      <li><a href="menu-template.php">Indian</a></li>
                      <li><a href="menu-template.php">Chinese</a></li>
                      <li><a href="menu-template.php">Mexican</a></li>
                      <li><a href="drink-template.php">Drinks</a></li>
                      <li><a href="menu-template.php">Desserts</a></li>
                    </ul>
                  </li>
                  <li><a href="">Reservation</a></li>
                  <li><a href="contact.php">Contact</a></li>
                </ul>
              </div><!-- ofest -->
            </div><!-- /row -->

        </div><!-- container -->
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
</header>
$(window).scroll(function(){
    if ($(this).scrollTop() > 300) {
       $('.navbar').addClass('fixed-menu');
    } else {
       $('.navbar').removeClass('fixed-menu');
    }
});

2 Answers

I am guessing you want your navbar to be fixed at the top.

It look likes you are using Bootstrap, then why go through so much trouble just add the class "navbar-fixed-top" to your navigation. ;)

<nav class="navbar navbar-default navbar-fixed-top">

Visit this link for more info http://www.w3schools.com/bootstrap/bootstrap_navbar.asp

Well, thanks for the answer but my question was "Why is this not addint the class to it? If i just add the calss with jQuery..."

I don't know if you missed the JS part :D But this is what im tryign to solve.

$(window).scroll(function(){
    if ($(this).scrollTop() > 300) {
       $('.navbar').addClass('fixed-menu');
    } else {
       $('.navbar').removeClass('fixed-menu');
    }
});

Although it works on codepen here

It doesn't work on my local site. I added it via console as well as i did from text editor adding only the class to it which worked, but this entire code doesn;t work in my local dev.

Oh sorry then :p Lets see if I can figure out the problem.

p.s I am not a pro :p