v2 documentation

This documentation is for an older version of scrollNav and provided for reference only. Version 2 is no longer supported and I highly recommend upgrading to Version 3.

Getting Started

Install with Bower bower install scrollNav

Or download the latest zipped package.

Requirements

Usage

Import

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>

Build your page

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-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>

Initialize

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.

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 (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 and sub-section that is the highest within the view bounds. An in-view class is attached to all nav items whose section or sub-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.

Destroy scrollNav

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');

Reset Positions on DOM Change

There are a couple of ways you can reset scrollNav’s positions when the DOM changes:

Manually

Simply call the resetPos method on your own:

$.fn.scrollNav('resetPos');

Automatically

Utilize mutation observers to call the resetPos method automatically:

var $;
var observer_target = document.querySelector('.post__article');
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function() {
    $.fn.scrollNav('resetPos');
  });
});
var observer_config = {
  attributes: true,
  childList: true,
  characterData: true,
  subtree: true
};
observer.observe(observer_target, observer_config);

Default options

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',
  className: 'scroll-nav',
  showHeadline: true,
  headlineText: 'Scroll To',
  showTopLink: true,
  topLinkText: 'Top',
  fixedMargin: 40,
  scrollOffset: 40,
  animated: true,
  speed: 500,
  insertTarget: this.selector,
  insertLocation: 'insertBefore',
  activeClass: 'active',
  arrowKeys: false,
  scrollToHash: true,
  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 selector, 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.

Class Name

If you want to change the default scroll-nav class naming used throughout (including in the BEM syntax for the child elems), you can do so by changing this value to your own.

Show Headline

Set this to false to remove the Headline Text entirely.

Headline Text

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.

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 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.

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 this to true to allow up/down arrow keys to jump through each section.

Active Class

By default, scrollNav will use the class ".active" to indicate which nav item is currently in view. If you need a different class name, add it here.

Scroll to hash

Set this to false if you use hashed (http://example.com/#home) values for routing.

Callback functions

There are three new callback functions that you can utilize to accommodate 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
  }
});

Errors

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.