- Overview
- Documents
scrollNav is a light jQuery plugin that grabs your page's existing content, divides it up into logical sections and builds a customizable scrolling sidebar navigation. Scroll this page and watch the nav follow along with you.
Features
-
Simple to setup
A single file import and function in your footer and you're on your way.
-
Fully Customizable
All the styling is up to you and the few bits controled by the plugin are editable
-
Tiny Size
Damn right it's fast! At 1.5kb min'd and GZip'd, scrollNav is pretty unintrusive.
Source: scrollnav.com
1. INCLUDE JS FILES
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script src="jquery.scrollNav.min.js"></script>
2. HTML
<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-heading">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>
Include a class or id hook on the element you want to apply the plugin to and include an <h2> for section headings you want to inlcude in the navigation.
3. JAVASCRIPT
$('.post__article').scrollNav();
The plugin scans the article, grabs all the <h2>s, adds them to the navigation list and inserts the list before the article.
4. OPTIONS
$('.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 });
Sections
As mentioned, the script automatically searches for <h2>s within the target article. If your page structure differs, feel free to target another element, like an <h3> or <h4> or even a class, like.scroll-headline.
Sub-sections
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.
Section Wrapper Element
If your article already contains section tags, you'll want to change this to 'div'. Sub-sections aren't affected by this option.
Show Headline
Set this to false to remove the Headline Text entirely.
Headline Text
scrollNav’s default headline text is ‘Scroll To’, but feel free to change it to whatever works for you, like ‘Article Sections’ or ‘Page Navigation’
Show Top Link
Set this to false to remove the Top nav item entirely.
Top Link Text
scrollNav’s default return to the top link text is ‘Top’, but feel free to change it to whatever works for you.
Fixed Margin
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.
Scroll Offset
This option affects two things. First is the "active state" boundries within the viewport. The bounderies are within the area from the top and bottom of the viewport offset by this amount. Second is the destination when scrolling the page. This will place the heading of the section right at the top edge of the "active state" boundry.
Animated Scrolling
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.
Scrolling Speed
Change this to either increase or decrease the animated page scroll speed.
Insertion Target
If you need to insert the nav relative to an element other than the one scrollNav is initialized on, you can change it here.
Insertion Location
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
Arrow Key Navigation
Set to false by default, change this to `true` to allow up/down arrow keys to jump through each section.
Callback functions
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 } });
5. STYLING
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
<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 orsn-failed if it fails