Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@apatheticwes/stickynav

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apatheticwes/stickynav

A simple navigation bar that efficiently attaches / detaches to the top of the viewport upon scrolling

  • 0.5.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 11 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc