A jQuery plugin for building a scrolling side navigation menu
Install with Bower bower install scrollNav
Or download the latest production version or the development version.
Check out the homepage to see it in action.
-
With a single file import and function in your footer, you're on your way.
-
Very little styling is set for you, but the bit that is, is fully customizable.
-
At 1.5kb min'd and GZip'd, scrollNav is pretty unintrusive. If you use Ajax to import and initiate it, it's almost negligable.
- For in-page usage jQuery 1.8.0 or greater and html5shiv/modernizr for older browser support
- To build source Node 0.10.0 or greater and grunt 0.4.0 or greater
Start by importing the script to your page, the best location is in the footer, but no matter what, make sure it follows your jQuery file.
<script src="jquery.scrollNav.min.js"></script>
Include a class or id hook on the element you want to apply the plugin to and include an <h2>
for each section you want to inlcude in the navigation.
<div class="main">
<article class="post__article">
<header class="post__header">
<h1 class="post__heading">This is the main heading for the article</h1>
<p class="post__sub-headling">This is a sub-heading for the article</p>
</header>
<p>Yada yada yada...</p>
<h2>This is a section heading</h2>
<p>More yada yada...</p>
<h2>Another section heading</h2>
<p>More more yada...</p>
</article>
</div>
Now initialize the plugin with your hook for the article
$('.post__article').scrollNav();
and the plugin scans the article, grabs all the <h2>
s, adds them to the navigation list and inserts the list before the article. It's that easy...well almost.
To keep the plugin simple there are no styles added to the navigation, that's all up to you. The nav structure looks like this and includes class names in the BEM Methodology style (for a good overview read MindBEMding - getting your head 'round BEM syntax):
<nav class="scroll-nav">
<div class="scroll-nav__wrapper">
<span class="scroll-nav__heading">
<ol class="scroll-nav__list">
<li class="scroll-nav__item">
<a class="scroll-nav__link">
An active
class is attached to the nav item matching the section that is the highest within the view bounds. An in-view
class is attached to all nav items whose section is within the view bounds. If you have short sections at the end of your page and dislike that the last nav itmes are never activated, you can use the in-view
hook to style all sections with in the view.
There are loading hooks added to the body element (similar to how Typekit handles font loading) to allow for css transitions or any other changes in css you'd need. When the plug-in starts sn-loading
is added to the body class and is replaced by sn-active
when the plugin is successful or sn-failed
if it fails.
In addition to the initialization, there is now a destroy method available should you need it. To destroy scrollNav and remove all it's DOM changes use:
$('.post__article').scrollNav('destroy');
There are a few customizable options in scrollNav using key : value pairs. These are the defaults.
$('.post-article').scrollNav({
sections: 'h2',
subSections: false,
sectionElem: 'section',
showHeadline: true,
headlineText: 'Scroll To',
showTopLink: true,
topLinkText: 'Top',
fixedMargin: 40,
scrollOffset: 40,
animated: true,
speed: 500,
insertTarget: this.selector,
insertLocation: 'insertBefore',
arrowKeys: false,
onInit: null,
onRender: null,
onDestroy: null
});
As mentioned, the script automatically searches for <h2>
s within the target article. If your page structure differs, feel free to target another selector, like an <h3>
or <h4>
or even a class, like .scroll-headline
.
Set to false
by default, the plugin supports nesting sub-sections within each section in the final nav. Available selectors are the same as Sections.
If your article already contains section
tags, you'll want to change this to 'div'
. Sub-sections aren't affected by this option.
Set this to false
to remove the Headline Text entirely.
scrollNav's default title text is 'Scroll To', but feel free to modify it to whatever works for you, like 'Article Sections' or 'Page Navigation'.
Set this to false
to remove the Top Link nav item entirely.
scrollNav's default return to the top link text is 'Top', but feel free to modify it to whatever works for you.
This is the top
dimension you set for the .scroll-nav.fixed
class, which is applied as the user scrolls down the page and is removed as they scroll above the article. You definitely want to set this if you don't use the default 40px, otherwise the nav will jump around as the user scrolls past the top of the article.
This option affects two things. First is the "active state" boundries within the viewport. The bounderies are within a distance from the top and bottom of the viewport equal to this amount. Second is the destination when animating the page scroll. This will place the heading of the section right at the top of the "active state" boundry.
The plugin animates the page scroll when clicking on a nav link by default. Set this to false
if you do not wish to animate the scroll.
Change this to either increase or decrease the animated page scroll speed.
If you need to insert the nav relative to an element other than the one scrollNav is initialized on, you can change it here.
You can pass any of the following jQuery insertion methods to change where scrollNav is inserted in relation to the targeted container. insertBefore
, prependTo
, appendTo
, or insertAfter
Set this to true
to allow up/down arrow keys to jump through each section.
There are three new callback functions that you can utilize to accomodate anything you need to run after specific scrollNav events. They are onInit
, onRender
, and onDestroy
. Add them to your options just like any other and they should look like this:
$('.post__article').scrollNav({
onInit: function() {
callback actions in here
}
});
The plugin will refuse to build and log an error message if it doesn't find your desired container, the insertion target or any of the headlines specified within the container. If the nav doesn't show up on load, check your browser's console.
There are a few known issues, including poor location updating when scrolling on touch devices. If you find any others please submit them to the issue tracker.
scrollNav is Copyright © 2012-2013 James Wilson, released under the MIT license. This means you can re-create, edit or share the plugin as long as you maintain the same open licensing.
Latest stable version is v2.1.1. Make sure to view the changelog before updating, v2 is a complete re-write of the plugin.
Tests are written using QUnit. To run the test suite with PhantomJS, run $ grunt test
or $ grunt watch
. To run the test in your default browser run $ grunt test:browser
.
Please read the contributing guidelines and issue tracker before starting on code.
In order to build and test scrollNav.js, you'll need to install its dev dependencies $ npm install
and have grunt-cli globally installed $ npm install -g grunt-cli
.
Available Grunt tasks that will be useful in development.
- grunt lint – Runs source and test files through JSHint.
- grunt test – Runs the test suite with PhantomJS.
- grunt concat - Builds the source to /dist.
- grunt uglify - Minifies the source to /dist.
- grunt build - Runs all of the above and rebuilds from source .
- grunt watch – Runs all of the above whenever a file is modified.