Exciting release!Introducing "safe npm". Learn more
Socket
Log inDemoInstall

@sidsbrmnn/scrollspy

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Issues
File Explorer

Advanced tools

@sidsbrmnn/scrollspy

A simple lightweight JavaScript library without jQuery or any other dependencies to handle scroll spying on static webpages.

    1.1.0latest
    GitHub

Version published
Maintainers
1
Weekly downloads
957
increased by13.12%

Weekly downloads

Changelog

Source

[1.1.0] - 2022-01-06

  • Added onActive callback function (#6)

Readme

Source

@sidsbrmnn/scrollspy

npm version

A simple lightweight JavaScript library without jQuery or any other dependencies to handle scroll spying on static webpages.

Install

  1. via NPM

    Install it using npm

    npm install --save @sidsbrmnn/scrollspy
  2. Use the minified script from unpkg.com

    <script src="https://unpkg.com/@sidsbrmnn/scrollspy@1.x/dist/scrollspy.min.js"></script>

Usages

Easy for using, syntax just like this:

scrollSpy(menu, options)

This little plugin will add active class into your existing menu item when you scroll your page to a matched section by ID. If you are giving it a try, make sure that you:

  1. Added CSS for active class in your menu item. Because, this plugin do NOT include CSS.

  2. Create your sections.

    Example: <section id="first-section">...</section>

  3. Added an attribute which is an section ID into your menu items. Default is href.

    Example: "href"="#first-section". You also replace href with the other name by hrefAttribute in options.

Arguments

  1. The menu is query selector to your menu. It is string or HTMLElement instance.

  2. The options is optional. It is type of object which contains properties below:

NameTypeDefaultDescription
sectionSelectorstringsectionQuery selector to your sections
targetSelectorstringaElement will be added active class
offsetnumber0Offset number
hrefAttributestringhrefThe menu item's attribute name which contains section ID
activeClassstringactiveActive class name will be added into targetSelector
isActivefunctionnullA callback method that's called with the current menuItem and section

ES6

import scrollSpy from '@sidsbrmnn/scrollspy' const options = { sectionSelector: 'section', // Query selector to your sections targetSelector: '.nav-link', // Query selector to your elements that will be added `active` class offset: 100 // Menu item will active before scroll to a matched section 100px } // Initialize scrollSpy(document.getElementById('navMain'), options) // Shorter way scrollSpy('#navMain', options)

Script tag

<script src="/path/to/dist/scrollspy.min.js"></script> <script> window.onload = function () { scrollSpy('#navMain', { sectionSelector: 'section', targetSelector: '.nav-link', offset: 100 }); } </script>

Licence

MIT

FAQs

Last updated on 06 Jan 2022

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket
support@socket.devSocket SOC 2 Logo

Product

  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc