1.0.1 • Published 2 years ago

vanilla-js-menu-scrollspy v1.0.1

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

Vanilla JS Menu Scrollspy

Vanilla JS Menu Scrollspy is a script that tracks website scrolling and automatically marks and unmarks menu links of website sections according to the current scrolling position.

Bundles

Inside the dist folder you will find four different bundles:

FilenameModule Type
menu-scrollspy.amd.min.jsAMD (Asynchronous Module Definition)
menu-scrollspy.esm.min.jsES Module (ES6 Module)
menu-scrollspy.iife.min.jsIIFE (Immediately Invoked Function Expression)
menu-scrollspy.min.jsUMD (Universal Module Definition)

Installation

Via CDN:

<script src="https://cdn.jsdelivr.net/npm/vanilla-js-menu-scrollspy@latest/dist/menu-scrollspy.min.js"></script>

Via Downloading / Self-Hosting:

<script src="/js/menu-scrollspy.min.js"></script>

Via npm:

npm i vanilla-js-menu-scrollspy --save-dev
import MenuScrollspy from 'vanilla-js-menu-scrollspy';

Usage

Include the script via CDN, downloading / self-hosting or install the package via npm and import the script as module.

Invoke the script after the DOM has been fully loaded ...

... with Vanilla JavaScript:

window.addEventListener('load', (event) => {
    const menuScrollspy = new MenuScrollspy();
}, {passive: true});

... with jQuery:

$(document).ready(function () {
    const menuScrollspy = new MenuScrollspy();
});

Add some styling with SCSS:

$accent: #64b5f6;

.menu-item {
  transition: color 0.25s ease-in-out;

  &:hover,
  &.active {
    color: $accent;
  }

  // also add styling for included icons / SVGs
}

Set custom config / options when needed.

API / Config / Options

You can pass an object with the following config / options to the new MenuScrollspy(); constructor to change the default behaviour of the script:

const menuScrollspy = new MenuScrollspy({
    // config / options
});
NameMeaningDefault ValueExample value(s)
classActiveThe class applied to the active menu link.'active''classname'
classMenuItemThe class by which menu items are identified.'menuitem''classname'
classSelectorThe class by which website sections are registered.'jsmenuscrollspy''classname'
customEventsEvents that should re-init the script (resize is already provided).[]'hyphenopolydone', 'lazyloadingdone'
enterViewportThresholdThe viewport line which website sections need to enter or leave to mark and unmark menu links.'middle''top'|'middle'|'bottom'
fixedMenuSelectorValid selector for document.querySelector() to get the height of a fixed menu and use it in the viewport threshold calculation.null'#nav'
reInitOnScrollIf scrolling should re-init the script (for example lazy loading content like images without placeholders changing the website height).falsetrue|false
updateUrlFragmentOnScrollIf scrolling should update the URL fragment (and the browser history).truetrue|false

Demo

License

The Vanilla JS Menu Scrollspy package is open-sourced software licensed under the MIT license.