jQuery One Page Navigation Plugin
When appropriate, I am a fan of the one-page sites. I really like the ones that add smooth scrolling and highlight the navigation depending upon which part of the page you have scrolled to. Here are a few examples: Brizk Design and Crush + Lovely. I finally have a freelance project where a one-page site makes sense, so I needed to write the JavaScript to make the navigation work how I wanted.
I wanted the page to scroll smoothly when the navigation was clicked, so I used the jQuery ScrollTo plugin. I also wanted the page to automatically highlight the correct navigation section depending upon which section was scrolled to, and that was where I added my custom code.
If you want to skip ahead, you can check out the demo and download the plugin from GitHub.
The Markup
I started with an unordered list for the navigation and a bunch of sections:
The JavaScript
Then, I added jQuery, the ScrollTo plugin, and my plugin to the page:
Finally, I just need to call my plugin on the navigation:
Options
There are a few options for this plugin:
- currentClass: 'current' Class to add to the list item when the navigation item is selected
- changeHash: false If you want the hash to change when the user clicks on the navigation, change this to true
- scrollSpeed: 750 Speed to scroll the page when the navigation is clicked
And that’s it. Check out the demo and download the plugin from GitHub.