0.5.8 • Published 7 years ago

@apatheticwes/stickynav v0.5.8

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

Sticky Nav

NPM Version License

A package of "Sticky Navigation" utilities

Introduction

A collection of 3 mini-utilities: a "sticky" Class to affix elements to the top of the viewport when scrolled, a function to smoothly scroll the page to any anchor, and a Class that auto-generates an in-page navigation from data-attribues in the DOM.

Scroll and Sticky may be used individually, but when used in tandem via StickyNav the result is an automatically created navigation menu that sticks to the top of the viewport, with nav-items that smoothly scroll to corresponding anchors in the page.

Installation

If you're oldskool, you can download the pre-built version to quickly mess around. Otherwise:

npm i @apatheticwes/stickynav -D

ES6

import { Sticky, Scroll, StickyNav } from 'stickynav';

CommonJS

var sticky = require('stickynav').Sticky;
var stickynav = require('stickynav').StickyNav;
var scroll = require('stickynav').Scroll;

Recipes

Create an auto-generated sticky navigation from in-page DOM elements:

<!-- this will become a sticky navigation, populated with nav items that initiate smooth scrolling when clicked -->
<ul id="sticky"></ul> 

...

<main>
  <section data-nav="Introduction">
  <section data-nav="Overview">
  <section data-nav="Summary">
</main>
new stickyNav({
  nav: '#sticky'
});

Or, just create a sticky element. This one is bounded by its parent (meaning it will unstick along with the bottom edge of its parent), and will become sticky 80px from the top of the viewport:

new Sticky('#sticky', {
  boundedBy: true,
  offset: 80
}

Smoothly scroll to a particular page section or anchor:

const intro = document.querySelector('#intro');

scrollPage(intro);

Options

Sticky

nametypedefaultdescription
elementHTMLElementThe element to sticky-ify (required).
option.boundedByboolean, HTMLElement"true" to use the parent node, or a custom bounding element for the sticky element. This affects where / when the sticky will attach and detach
option.offsetnumber0An offset from the top at which to toggle "stickiness"

Scroll

nametypedefaultdescription
toHTMLElementThe DOM node to scroll to (required)
offsetnumber0A scrolling offset
callbackfunctionCallback function when scrolling is complete

StickyNav

nametypedefaultdescription
option.navHTMLElementThe DOM node to generate the nav into (required)
option.sectionsstring, HTMLElementdata-navA querySelector, or the elements themselves, to be used when generating the menu items in the navigation
option.boundedByboolean, HTMLElementfalse"true" to use the parent node, or a custom bounding element for the sticky element. This affects where / when the sticky will attach and detach
option.offsetnumber0The scroll / sticky offset

Examples

There is a demo is available. Alternatively, after cloning the repo, try:

npm i
npm start

A server will spin up at http://localhost:8080, where you may play with the various examples.

Support

  • IE8+
  • Safari / Chrome
  • Firefox
  • iOS
  • Android

License

MIT