Simple ScrollSpy Plugin With Pure JavaScript

Category: Javascript , Menu & Navigation | May 11, 2018
Author: huukimit
Views Total: 535
Official Page: Go to website
Last Update: May 11, 2018
License: MIT


Simple ScrollSpy Plugin With Pure JavaScript


A simple, lightweight JavaScript library that applies the Scrollspy functionality to one page scrolling website.

The plugin automatically adds the CSS class ‘active’ to the navigation item when the page is scrolled to its corresponding section.

How to use it:

Install the library.

# Yarn
$ yarn add simple-scrollspy

$ npm install simple-scrollspy --save

Or download the zip and then insert the JavaScript file ‘simple-scrollspy.js’ into the document.

<script src="simple-scrollspy.js"></script>

Create a regular navigation menu and specify the target page sections using the ‘data-scrollspy’ attribute as follows:

<section class="scrollspy" id="home">
  <nav class="navigation">
    <ul id="menu" class="menu">
      <li class="menu__item">
          <a href="#home" class="active" data-scrollspy="#home">Home</a>
      <li class="menu__item">
          <a href="#contact" data-scrollspy="#contact">Contact</a>
      <li class="menu__item">
          <a href="#about" data-scrollspy="#about">About</a>

<section class="scrollspy" id="contact">
  Contact Setion

<section class="scrollspy" id="about">
  About Setion

Active the scrollspy plugin with default options.

window.onload = function () {

All possible options to customize the scrollspy functionality.

window.onload = function () {
  scrollSpy('#menu', {
    offset: 0, // in pixels
    menuActiveTarget: 'li > a',
    sectionClass: '.scrollspy',
    activeClass: 'active',

Customize the styles when a menu item is active.

.menu__item {
  color: #ff7600;
  font-weight: bolder;

Leave a Reply