Skip to content
Advertisement

How to make a one-pager navigation with waypoints to enable/disable nav elements?

I’m stuck at my one-pager navigation logic. As you can see in my detailed example below, I already have a dynamic navigation depending on the scroll position. Also, when you click a navigation item, you are getting scrolled down to the specific section.

What I’m stuck at is the point to add/remove the active class to the navigation item. Somehow, I can’t find a logic in my brain to make this a smart part of my navigation. I know that I can make some static JS code, but that’s not what I want.

At least, I just want to add another menu item and an element containing the data-waypoint attribute.

It’s not an option to add this to the item click functionality, since the user can scroll down manually, which then don’t get recognized!

JavaScript
JavaScript
JavaScript

Advertisement

Answer

I’m happy to share my answer I’ve found. I was first looking for a simple solution, but ended up using Waypoints jQuery library.

This is my solution. I hope it helps someone:

JavaScript
JavaScript
JavaScript

Also, I’ve wrapped the nav items into a wrapper and set the margin to the outer nav. During page init and resize, I’m setting a min-width to the outer nav to prevent any content jumping and issues with the waypoints. Otherwise, the offset and all the other calculation stuff makes a lot of problems.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement