npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vanilla-js-menu-scrollspy

v1.0.1

Published

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.

Downloads

14

Readme

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:

| Filename | Module Type | | -------------------------- | ------------------------------------------------------------- | | menu-scrollspy.amd.min.js | AMD (Asynchronous Module Definition) | | menu-scrollspy.esm.min.js | ES Module (ES6 Module) | | menu-scrollspy.iife.min.js | IIFE (Immediately Invoked Function Expression) | | menu-scrollspy.min.js | UMD (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
});

| Name | Meaning | Default Value | Example value(s) | | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | --------------------------------------------------------- | | classActive | The class applied to the active menu link. | 'active' | 'class‑name' | | classMenuItem | The class by which menu items are identified. | 'menu‑item' | 'class‑name' | | classSelector | The class by which website sections are registered. | 'js‑menu‑scrollspy' | 'class‑name' | | customEvents | Events that should re-init the script (resize is already provided). | [] | ['hyphenopoly‑done', 'lazyloading‑done'] | | enterViewportThreshold | The viewport line which website sections need to enter or leave to mark and unmark menu links. | 'middle' | 'top'|'middle'|'bottom' | | fixedMenuSelector | Valid selector for document.querySelector() to get the height of a fixed menu and use it in the viewport threshold calculation. | null | '#nav' | | reInitOnScroll | If scrolling should re-init the script (for example lazy loading content like images without placeholders changing the website height). | false | true|false | | updateUrlFragmentOnScroll | If scrolling should update the URL fragment (and the browser history). | true | true|false |

Demo

License

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