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

Jack Spangenberg
Jack Spangenberg
643 Points

jQuery Waypoints change navbar styling on scroll

I have the following navbar code with BS4

    <div id="navbar">
        <nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-inverse hidden-sm-down bg-custom">
          <div class="container-fluid">
            <a class="navbar-brand" href="#">
              <p class="icon-sblack icon-brand text-white my-0">
            </a>
            <ul class="navbar-nav navbar-collapse justify-content-end mx-3">
              <li class="nav-item">
                <a class="nav-link" href="#top">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#about">About</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Portfoio</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Contact</a>
              </li>
            </ul>
          </div>
        </nav>
      </div>

    <div id="changenav" style="margin-top: 50px; height: 500px; width: 100%; background-color: black;"></div

I'd like it so the navbar doesn't show until the visitor scrolls down, then I'd like the navbar to become black on white after they scroll past an element with the name #scrollPast instead of white on black. How can I do this? I am already sourcing jQuery.

    var waypoint = new Waypoint({
      element: document.getElementById('changenav'),
      handler: function(direction) {
          $(".navbar").removeClass("bg-inverse");
          $(".navbar").removeClass("navbar-dark");
          $(".navbar").removeClass("bg-custom");
          $(".navbar").addClass("navbar-light");
          $(".navbar").addClass("scrolled-nav");
        } else {
          $(".navbar").addClass("bg-inverse");
          $(".navbar").addClass("navbar-dark");
          $(".navbar").addClass("bg-custom");
          $(".navbar").removeClass("navbar-light");
        }
      }
    })

I am using the jQuery waypoints plugin.